- Yii 1.1 Application Development Cookbook
- Alexander Makarov
- 391字
- 2021-04-02 18:41:02
Reusing views with partials
Yii supports partials, so if you have a block without much logic that you want to reuse or want to implement e-mail templates, partials are the right way to look.
Getting ready
- Set up a new application using
yiic webapp
. - Create a
WebsiteController
as follows:class WebsiteController extends CController { function actionIndex() { $this->render('index'); } }
How to do it...
We will start with a reusable block. For example, we need to embed a YouTube video at several website pages. Let's implement a reusable template for it.
- Create a view file named
protected/views/common/youtube.php
and paste an embed code from YouTube. You will get something like:<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/S6u7ylr0zIg?fs=1 "></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/S6u7ylr0zIg?fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>
- Now, we need to make it reusable. We want to be able to set video ID, width, and height. Let's make width and height optional, as follows:
<object width="<?php echo!empty($width) ? $width : 480?>" height="<?php echo!empty($height) ? $height: 385?>"><param name="movie" value="http://www.youtube.com/v/<?php echo $id?>?fs=1 "></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/<?php echo $id?>?fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="<?php echo !empty($width) ? $width : 480?>" height="<?php echo !empty($height) ? $height: 385?>"></embed></object>
- Now, you can use it in your
protected/views/website/index.php
like this:<?php $this->renderPartial('////common/youtube', array( 'id' => '8Rp-CaIKvQs', // you can get this id by simply looking at video URL 'width' => 320, 'height' => 256, ))?>
Looks better, right? Note that we have used
//
to reference a view. This means that Yii will look for a view starting fromprotected/views
not taking controller name into account. - Now, let's send some e-mails. As we are unable to write unique letters to thousands of users, we will use a template but will make it customized. Let's add a new method to
protected/controllers/WebsiteController.php
as follows:class WebsiteController extends CController { function actionSendmails() { $users = User::model->findAll(); foreach($users as $user) { $this->sendEmail('welcome', $user->email, 'Welcome to the website!', array('user' => $user)); } echo 'Emails were sent.'; } function sendEmail($template, $to, $subject, $data) { mail($to, $subject, $this->renderPartial('//email/'.$template, $data, true)); } }
- Here is our template
protected/views/email/welcome.php
:Hello <?php echo $user->name?>, Welcome to the website! You can go check our new videos section. There are funny raccoons. Yours, Website team.
How it works...
CController::renderPartial
does the same template processing as CController::render
except the former does not use layout. As we can access current controller in a view using $this
, we can use its renderPartial
to use view within another view. renderPartial
is also useful when dealing with AJAX as you don't need layout rendered in this case.
There's more…
For further information, refer to the following URL:
http://www.yiiframework.com/doc/api/CController/#renderPartial-detail
See also
- The recipe named Using controller context in a view in this chapter
- iPad+Procreate數字插畫設計案例教程(全彩微課版)
- ERP沙盤模擬簡明教程
- 數字孿生體:第四次工業革命的通用目的技術
- YUI 2.8: Learning the Library
- SolidWorks2014基礎實例教程
- PyTorch深度學習簡明實戰
- 中文版Photoshop CC平面設計實用教程
- Altium Designer 21 PCB設計官方指南(高級實戰)
- Unity 3D游戲開發(第2版)
- Microsoft SharePoint 2010 and Windows PowerShell 2.0: Expert Cookbook
- Apache JMeter
- Learning Ext JS
- UG NX 10中文版完全自學手冊
- 3ds Max 2014/VRay效果圖制作實戰從入門到精通
- CorelDRAW X7中文版基礎教程