Pass variables to layout - take care of metatags

How can I pass values to the layout view? I have to put specific keywords and description for every page.

Hi,

http://www.yiiframew…41.msg9819.html

hope this helps

They deal with registering different JS script. This is done using ClientScript::registerXXX(), i know this and use it. I need to render different metatags for each page. Any more ideas?

I solved this as following:

  1. Define $_description and/or $_keywords properties in the controllers I actually need them

  2. Set their value

  3. Define getter methods (getDescription(){return $this->_description;}

  4. In the layout:

This works fine for me, but I'd like to do the same with just exporting values, just like to any view.

Did you do this in base CExtController?

I am taking little different approach. Sharing here to see if this is a good idea. Comments please.

  1. In my SiteController, I defined two class variables

    public $pageDescription = 'this is default description goes here';

    public $pageKeywords  = 'this are default keywords';

  1. In the layout's head I added

  <meta name="description" content="<?=$this->pageDescription?>" />

  <meta name="keywords" content="<?=$this->pageKeywords;?>" />

  1. In my view. This is because the meta tag variables can be built dynamically from the controller and passed to the view.

<?php $this->pageTitle='This is page title'; ?>

<?php $this->pageDescription='description for this view'; ?>

<?php $this->pageKeywords='keyword1, keyword2,'.$more_keywords_from_the_controller; ?>

<div id="view_us_content">

  content goes here

</div>

 

Actually, it's nearly the same, except 3 things:

  1. Seems you extend all controllers from SiteController. I don't. So in order to prevent defining properties where I don't need them, I just check if such property exists. For thaty function to work, property should be defined as getter/setter.

  2. You assign values in the view. I have complex algorithm for title, keywords and description generation, so I set these values in controller

  3. The simplicity in the layout is because of (1).

Actually, approach is the same - we pull data from controller rather than pushing it to layout.

Agreed to your points.

In step 3.  <?php $this->pageKeywords='keyword1, keyword2,'.$more_keywords_from_the_controller; ?>

$more_keywords_from_the_controller; is coming from the controller.

You can't use $more_keywords_from_the_controller simply because you can't pass variable to the layout. If that was possible, there would be no need in these tricks

;D You tip made my code simpler - Thanks

Now I have those variable only in the Layout, Passed directly from the controller. With that I do not have to types those three lines in the view any more. It got reduced to two steps.

Quote

;D You tip made my code simpler - Thanks

Now I have those variable only in the Layout, Passed directly from the controller.

Do you mean you populate properties from controller rather than from layout? Or you can pass variable to the layout like you do to the view?

It may not be very clean, but it works! I have to define these two variables in every controller

    public $pageDescription = null; //Default

    public $pageKeywords    = null; //Default

Note: _pageTitle is defined the CController, which gets inherited by the extended class.

For the above two, I do not have  make any changes to the base code, add Setter or Getter.  Just define in every controller. I have only 6 projected controllers. So it is not pain for me.

Here is the simplest example of the actual action where the meta tags are built before invoking the render

My View is simple:

<div id="home-content">

  <table summary='dd'>

    <tr>

      <td>

            <h6>US States in alpha order with number of <?=$catname?></h6><hr class="cg" />

            <? $this->widget('application.components.location.ListAllStates'); ?>

      </td>

      <td style="width:320px">

            <h6>Ads</h6> <hr class="cg" />

            <? $this->widget('application.components.ads.Ads300x100IMU', array('naic'=>$naic, 'slot' => 1)); ?>

            <? gfAds300x250();?>

      </td>

      <td>

            <h6>Cities ordered by number of restaurants</h6><hr class="cg"/>

            <? $this->widget('application.components.location.ListMostActiveCities'

                          , array('naic' => $naic)); ?>

      </td>

    </tr>

  </table>

</div>

And I am using those meta vars directly in the Layout. It works!

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

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

<head>

  <title><?php echo $this->pageTitle; ?></title>

  <meta name="description" content="<?=$this->pageDescription?>" />

  <meta name="keywords" content="<?=$this->pageKeywords;?>" />

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

Implemented, working example at c2eat.com.

by the way, I could never get the urlManager to work :(

ClientScript::registerMetaTag() is solution.

http://www.yiiframew…rMetaTag-detail