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

Using the 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/'),
       ),
    ))?>
  3. In order to test it you can follow /index.php?r=website/index.

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. 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

主站蜘蛛池模板: 孟津县| 确山县| 时尚| 宜兴市| 虹口区| 汝南县| 司法| 塔城市| 丽江市| 巴中市| 峡江县| 五常市| 中西区| 慈利县| 乌拉特中旗| 承德县| 施秉县| 浑源县| 萨嘎县| 安图县| 青州市| 布尔津县| 鞍山市| 舞钢市| 岚皋县| 郯城县| 阜新| 札达县| 嘉黎县| 舟山市| 香河县| 玉溪市| 左贡县| 施甸县| 莆田市| 安远县| 凤冈县| 探索| 长宁区| 武强县| 藁城市|