官术网_书友最值得收藏!

Using controller context in a view

Yii views are pretty powerful and have many features. One of them is that you can use controller context in a view. So, let's try it.

Getting ready

Set up a new application using yiic webapp.

How to do it...

  1. Create a controller as follows:
    class WebsiteController extends CController
    {   
       function actionIndex()
       {
          $this->pageTitle = 'Controller context test';
          $this->render('index');
       }
    
       function hello()
       {
          if(!empty($_GET['name']))
             echo 'Hello, '.$_GET['name'].'!';
       }
    }
  2. Now, we will create a view showing what we can do:
    <h1><?php echo $this->pageTitle?></h1>
    <p>Hello call. <?php $this->hello()?></p>
    <?php $this->widget('zii.widgets.CMenu',array(
    'items'=>array(
          array('label'=>'Home', 'url'=>array('index')),
          array('label'=>'Yiiframework home', 'url'=>'http://yiiframework.ru/',
       ),
    ))?>

How it works...

We are using $this in a view to refer to a currently running controller. When doing it, we can call a controller method and access its properties. The most useful property is pageTitle which refers to the current page title and there are many built-in methods that are extremely useful in views such as renderPartials and widget.

There's more…

The following URL contains API documentation for CController where you can get a good list of methods you can use in your view:

http://www.yiiframework.com/doc/api/CController

主站蜘蛛池模板: 龙口市| 渭南市| 辰溪县| 鹰潭市| 图木舒克市| 连州市| 雅江县| 巴林右旗| 吉水县| 宁南县| 嘉峪关市| 杭锦后旗| 津南区| 普安县| 山阳县| 金秀| 扎囊县| 广南县| 扎赉特旗| 东光县| 栾城县| 唐海县| 彰化县| 且末县| 资兴市| 鹤山市| 宁乡县| 遂昌县| 扎囊县| 山西省| 海丰县| 凤山市| 长岭县| 新化县| 江安县| 抚远县| 梨树县| 稻城县| 健康| 南召县| 华宁县|