官术网_书友最值得收藏!

Defining multiple layouts

Most applications use a single layout for all their views. However, there are situations when multiple layouts are needed. For example, an application can use different layouts on different pages: two additional columns for blogs, one additional column for articles, and no additional columns for portfolios.

Getting ready

Set up a new application using yiic webapp.

How to do it...

  1. Create two layouts in protected/views/layouts: blog and articles. Blog will contain the following code:
    <?php $this->beginContent('//layouts/main')?>
    <div>
    <?php echo $content?>
    </div>
    <div class="sidebar tags">
       <ul>
          <li><a href="#php">PHP</a></li>
          <li><a href="#yii">Yii</a></li>
       </ul>
    </div>
    <div class="sidebar links">
       <ul>
          <li><a >Yiiframework</a></li>
          <li><a >PHP</a></li>
       </ul>
    </div>
    <?php $this->endContent()?>
  2. Articles will contain the following code:
    <?php $this->beginContent('//layouts/main')?>
    <div>
    <?php echo $content?>
    </div>
    <div class="sidebar toc">
       <ul>
          <li><a href="#intro">1. Introduction</a></li>
          <li><a href="#quick-start">2. Quick start</a></li>
       </ul>
    </div>
    <?php $this->endContent()?>
  3. Create three controllers named BlogController, ArticleController, and PortfolioController with index actions in all three:
    class BlogController extends Controller
    {
       function actionIndex()
       {
          $this->layout = 'blog';
          $this->render('//site/index');
       }
    }
    
    class ArticleController extends Controller
    {
       function actionIndex()
       {
          $this->layout = 'articles';
          $this->render('//site/index');
       }
    }
    
    class PortfolioController extends Controller
    {
       function actionIndex()
       {
          $this->render('//site/index');
       }
    }
  4. Now try http://example.com/blog, http://example.com/article, and http://example.com/portfolio.

How it works...

We defined two additional layouts for the blog and articles. As we don't want to copy and paste common parts from the main layout, we apply additional layout decorators using $this->beginContent and $this->endContent, as shown in the following diagram:

How it works...

So, we use a view rendered inside articles layout as the main layout's $content.

See also

  • The Using the controller context in a view recipe
  • The Using decorators recipe
主站蜘蛛池模板: 临安市| 黄石市| 桂东县| 丹巴县| 瓮安县| 阿克苏市| 安图县| 邳州市| 巫溪县| 博罗县| 吕梁市| 泰来县| 桃园县| 梁河县| 安陆市| 婺源县| 阿拉善右旗| 乐都县| 永德县| 南平市| 尖扎县| 潜山县| 尼木县| 遵化市| 进贤县| 道孚县| 永德县| 奇台县| 铁岭市| 中卫市| 尚志市| 依兰县| 普兰店市| 读书| 龙井市| 合山市| 定安县| 独山县| 潼关县| 广安市| 北碚区|