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

Using clips

One of the Yii features you can use in your views is clips . The basic idea is that you can record some output and then reuse it later in a view. A good example would be defining additional content regions for your layout and filling them elsewhere.

Getting ready

Set up a new application using yiic webapp.

How to do it...

  1. For our example, we need to define two regions in our layout: beforeContent and footer. Open protected/views/layouts/main.php and insert the following code line just before the content output (<?php echo $content; ?>):
    <?php if(!empty($this->clips['beforeContent'])) echo $this->clips['beforeContent']?>

    Then, insert the following into <div id="footer">:

    <?php if(!empty($this->clips['footer'])) echo $this->clips['footer']?>
  2. That is it! Now, we need to fill these regions somehow. We will use a controller action for the beforeContent region. Open protected/controllers/SiteController.php and add the following code to actionIndex:
    $this->beginClip('beforeContent');
    echo 'Your IP is '.Yii::app()->request->userHostAddress;
    $this->endClip();
  3. As for footer, we will set its content from a view. Open protected/views/site/index.php and add the following code:
    <?php $this->beginClip('footer')?>
    This application was built with Yii.
    <?php $this->endClip()?>
  4. Now, when you open your website's index page, you should get your IP just before the page content and a "built with" note in the footer.

How it works...

We mark regions with the code that just checks for existence of a clip specified, and if the clip exists, the code outputs it. Then, we record content for clips we defined using special controller methods named beginClip and endClip.

See also

  • The Using the controller context in a view recipe
主站蜘蛛池模板: 凤山县| 鹤岗市| 油尖旺区| 九江市| 奉贤区| 静海县| 寿宁县| 奇台县| 宿迁市| 新郑市| 合川市| 西安市| 临江市| 略阳县| 兴城市| 漳州市| 上林县| 仁寿县| 宣威市| 大荔县| 门源| 司法| 读书| 达拉特旗| 武乡县| 清水河县| 湟中县| 大关县| 工布江达县| 米泉市| 雷波县| 高州市| 都昌县| 连山| 南木林县| 江陵县| 克什克腾旗| 尖扎县| 洱源县| 赤壁市| 阳原县|