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 go about this.
Getting ready
Set up a new application using
yiic webapp.Create
WebsiteControlleras follows:class WebsiteController extends CController { function actionIndex() { $this->render('index'); } }Set up a database using the following SQL:
CREATE TABLE `user` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(200) NOT NULL, `email` varchar(200) NOT NULL, PRIMARY KEY (`id`) );
Use Gii to generate the
Usermodel.Add some data to the
usertable.
How to do it...
We will start with a reusable block. For example, we need to embed a YouTube video on several website pages. Let's implement a reusable template for it.
Create a view file named
protected/views/common/youtube.phpand paste an embed code from YouTube. You will get something like the following:<object width...