Using decorators
In Yii, we can enclose content into a decorator. The common usage of decorators is layout. Yes, when you are rendering a view using the render method of your controller, Yii automatically decorates it with the main layout. Let's create a simple decorator that will properly format quotes.
Getting ready
Set up a new application using yiic webapp.
How to do it...
First, we will create a decorator file,
protected/views/decorators/quote.php:<div class="quote"> “<?php echo $content?>”, <?php echo $author?> </div>
Now in
protected/views/site/index.php, we will use our decorator:<?php $this->beginContent('//decorators/quote', array('author' => 'Edward A. Murphy'))?> If anything bad can happen, it probably will <?php $this->endContent()?>Now, your home page should include the following markup:
<div class="quote"> “If anything bad can happen, it probably will”, Edward A. Murphy </div>