Yii Framework Forum: How automatically pass variable to view template? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How automatically pass variable to view template? Rate Topic: -----

#1 User is offline   srigi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 14-April 10

Posted 28 July 2011 - 04:17 AM

Hello,

is it possible automatically pass some particular variable(s) to View template in base Controller? I'm annoyed always doing this in my templates:

<?php $formater = Yii::app()->localeFormater; ?>

...

<h3><?= $post->title ?></h3>
<span class"date"><?= $formater->date($post->created) ?></span>

...


Either I fetch localeFormater in template by static call to CWebApplication, or I can pass it in Controller:

public function actionFoo()
{
    ...

    $this->render('foo', array(
        'post' => $post,
        'formater' => Yii::app()->localeFormater,
    ));
}



I don't like none of the above soutions. It would be nice to be able pass some common variables to template in univesal parent (Controller extends CController) of all Controllers, thus not altering code in actions/templates. Is this possible in Yii?
0

#2 User is offline   Ivica 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 317
  • Joined: 25-May 11

Posted 28 July 2011 - 04:45 AM

You can define controller public property(variable), and it will be visible in all views, that this controller render.

For example:
class ContactController extends Controller
{
    public $breadcrumb = "Contacts";
...
...
...

Zurmo: Yii Powered Open Source CRM
zurmo.org
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users