Requiring

Hi,

How do I render a partial view in the application layout?

Anyone not know what I mean by partial view?

Also I have another which could work the same way with my application.

Where can I write "global" code that executes at roughly the same time as the layout?

Basically …





<?php


if($x == null) $company = "test";


?>


<html>


<head>


etc




The above is the affect I need to create.

$this->renderPartial();

means that controller renders view withot layouth

Hi, is there anyway to do a render partial inside the layout file?

You should use renderFile


$this->renderFile(Yii::app()->getPathOfAlias('application...'));

Anyway this is not the standard way. Is much more confortable to make a widget and use the render of the widget (that is by default a partial view)

Yeah, basically folks what I am trying to create is a file, with some code in it, that sets some variables and which also runs whenever the application is started i.e. on every page. Any ideas?

You’re really after two things, I think. :)

Your main layout is run each request, and with the content in it.

You can use that for site common layout. That’s what’s it for.

If you want code that’s common to all controllers, that is: common logic, you could just create an ExtendedController, like demonstrated here:

http://www.yiiframew…ified-meta-tags

And/or implement a filter (which is a good alternative to the beforeRender approach, and more flexible).

Let me show you …




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


	<html xmlns="http://www.w3.org/1999/xhtml">


		<head>

		

			<title><?php echo CHtml::encode($this->pageTitle); ?></title>

			<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

			<meta name="description" content="You are looking at <?php echo CHtml::encode($this->pageTitle); ?>, the application that manages your finance business." />

			<link href="<?php echo Yii::app()->request->baseUrl; ?>/css/style.css" rel="stylesheet" type="text/css" />

			

		</head>

		

		<body>

		

			<div id="menu">

				

				<?php

				

					if(!Yii::app()->user->isGuest) {

					

						$user = new User;

						$user = $user->findByPK(Yii::app()->user->id);

						Yii::app()->params["loggedInUser"] = $user;

					

						$company = new Company;

						$company = $company->findByPK($user->fk_id_with_company_id);

						Yii::app()->params["loggedInUsersCompany"] = $company;

						

					}

					

				?>

				

				<?php echo "<h1>" . Yii::app()->params["loggedInUsersCompany"]->name . "</h1>"; ?>

			

				<?php

				

					$this->widget("zii.widgets.CMenu", array(

						

						"items" => array(

							

							array("label" => "Log in", "url" => array("account/login"), "visible" => Yii::app()->user->isGuest),

							array("label" => "Sign up", "url" => array("account/signup"), "visible" => Yii::app()->user->isGuest),

							//array("label" => "Company info", "url" => array("account/editcompany"), "visible" => $company_info),

							array("label" => "My info", "url" => array("account/edituser"), "visible" => !Yii::app()->user->isGuest),

							array("label" => "Sign out", "url" => array("account/logout"), "visible" => !Yii::app()->user->isGuest)

							

						)

						

					));

				

				?>

			

			</div>

			

			<div id="submenu">

						

				<?php

				

					$this->widget("zii.widgets.CMenu", array(

						

						"items" => array(

							

							array("label" => "Dashboard", "url" => array("site/index"), "visible" => !Yii::app()->user->isGuest)

							

						)

						

					));

				

				?>

			

			</div>

			

			<div id="content">

			

				<div id="main">

				

					<?php echo $content; ?>

					

				</div>

				

			</div>

	

		</body>


	</html>



The following chunk of code taken out of the above, needs to be run to set the $user and $company globally for the application.




<?php

				

					if(!Yii::app()->user->isGuest) {

					

						$user = new User;

						$user = $user->findByPK(Yii::app()->user->id);

						Yii::app()->params["loggedInUser"] = $user;

					

						$company = new Company;

						$company = $company->findByPK($user->fk_id_with_company_id);

						Yii::app()->params["loggedInUsersCompany"] = $company;

						

					}

					

				?>



Technically that should work if I am using that layout, but what if I want to use other layouts.

It either needs to be in its own file and called from sort of config.

OR

It needs to be a partial view file, and called into the alternate layouts by $render->partial etc.

Take a closer look at the wiki article I linked to. :)

This is an excerpt from my Controller:


	public $pageTitle;

	public $pageKeywords;


	public $pageDescription;


	public function beforeRender() {


    	$this->pageTitle = Yii::app()->params['title'] . ' ' . Yii::app()->params['description'];


    	if (!empty($this->pageDescription)) {

        	Yii::app()->clientScript->registerMetaTag($this->pageDescription, 'description');

    	}

    	if (!empty($this->pageKeywords)) {

        	Yii::app()->clientScript->registerMetaTag($this->pageKeywords, 'keywords');

    	}


    	return true;

	}



That’s one piece of the puzzle.

Another piece would be using widgets. :)

They are separate from the themes/views/layouts - although they can be themed as well -> portlets.

But they’re good for reusable elements with logic.

However, since you are free to call renderContent on anything, you could - as yet another option - call renderContent on whatever you want.

Hmm, I will probably place it in a beforeFilter in the application controller so it acts on all the application.

Or I might use renderContent if the beforeFilter does not work. Thanks.

All my controllers inherit from Controller which itself inherits from ExendedController, so it’s touching the entire application.

It’s just one way of using inheritance. ;)

You can also write a behavior for application if you need some code to be executed each times.

For example:




?php


/**

 * ApplicationConfigBehavior is a behavior for the application.

 * It loads additional config paramenters that cannot be statically 

 * written in config/main

 */

class ApplicationConfigBehavior extends CBehavior

{

	/**

	 * Declares events and the event handler methods

	 * See yii documentation on behaviour

	 */

	public function events()

	{

		return array_merge(parent::events(), array(

			'onBeginRequest'=>'beginRequest',

		));

	}


	/**

	 * Load configuration that cannot be put in config/main

	 */

	public function beginRequest()

	{

		//Yii::app()->cache->flush();

		// load parameters from db

		// set theme

		if ($this->owner->user->getState('theme'))

			$this->owner->theme=$this->owner->user->getState('theme');		

		else

			Yii::app()->theme='classic';


		// set the user language

		

		//if the user choosed a language, we use the choosen one

		if ($this->owner->user->getState('applicationLanguage'))

			$this->owner->language=$this->owner->user->getState('applicationLanguage');

		else //otherways, we use the preferred language in browser

		{	/* unluckyly, yii actually uses a language code different from 

			 * the browser one, and so we have to translate them manually */

			switch (Yii::app()->request->preferredLanguage)

			{

				case 'it_it':

					$this->owner->language='it';

				break;

				case 'ru_ru':

					$this->owner->language='ru';

				break;

				

			}

		}

		$this->owner->name=Yii::t('main', 'Maple');

	}

}




This is used for set globally the theme, the language and the application name according to the user state.

You can include in main php with:





	/* the behavior that loads other paramenters that can be loaded only runtime

	 * see protected/components/ApplicationConfigBehavior.php */

	'behaviors'=>array(

		'runTimeConfig'=>array(

			'class'=>'ApplicationConfigBehavior'

		)

	),