- 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
推薦閱讀
- 常用工具軟件案例教程
- Photoshop CC實戰(zhàn)從入門到精通
- 剪映AI視頻剪輯:AI腳本+AI繪畫+圖文生成+數(shù)字人制作
- Wordpress 3 Complete
- AutoCAD Civil 3D 2018 場地設(shè)計實例教程
- Zenoss Core Network and System Monitoring
- 中文版Photoshop CS6平面設(shè)計實用教程(第2版)
- 無師自通AutoCAD:中文版室內(nèi)設(shè)計
- Photoshop CC摳圖+修圖+調(diào)色+合成+特效實戰(zhàn)視頻教程
- 短視頻剪輯基礎(chǔ)與實戰(zhàn)應(yīng)用(剪映電腦版)
- 音樂日記:Studio One 6場景×風(fēng)格編曲實用教程
- AI繪畫與攝影實戰(zhàn)108招:ChatGPT+Midjourney+文心一格
- Adobe創(chuàng)意大學(xué)Premiere Pro影視剪輯師標(biāo)準(zhǔn)實訓(xùn)教材(CS6修訂版)
- AutoCAD 2010 建筑設(shè)計與制作技能基礎(chǔ)教程
- MATLAB 2008全程指南