- Yii 1.1 Application Development Cookbook
- Alexander Makarov
- 244字
- 2021-04-02 18:41:02
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 will 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...
- For our example, we need to define two regions in our layout:
beforeContent
andfooter
. Openprotected/views/layouts/main.php
and insert the following 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']?>
- That is it. Now, we need to fill these regions somehow. We will use a controller action for the
beforeContent
region. Openprotected/controllers/SiteController.php
and add the following toactionIndex
:$this->beginClip('beforeContent'); echo 'Your IP is '.Yii::app()->request->userHostAddress; $this->endClip();
- As for footer, we will set its content from a view. Open
protected/views/site/index.php
and add the following:<?php $this->beginClip('footer')?> This application was built with Yii. <?php $this->endClip()?>
- Now, when you open your website's index page, you should get your IP just before the page content and "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 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 recipe named Using controller context in a view in this chapter
推薦閱讀
- Oracle SOA Suite Developer's Guide
- 從零開始:Flash CS6中文版基礎培訓教程
- 數(shù)字孿生體:第四次工業(yè)革命的通用目的技術
- COSPLAY的后期藝術:Lightroom+Photoshop修圖技法攻略
- 詳解AutoCAD 2022機械設計(第6版)
- PowerPoint 2016實戰(zhàn)從入門到精通(超值版)
- Django 1.0 Template Development
- RAW 數(shù)碼底片演義
- 修片有道:PHOTOSHOP攝影后期專業(yè)技法
- 剪映真?zhèn)鳎?8招玩轉短視頻剪輯
- Photoshop CS6平面設計實戰(zhàn)從入門到精通(經典暢銷版)
- Photoshop CS6淘寶美工完全實例教程(培訓教材版)
- 中文版Illustrator CS6基礎培訓教程(第2版)
- 3ds Max 2014/VRay效果圖制作實戰(zhàn)從入門到精通
- Moodle 1.9 Multimedia