`
hudeyong926
  • 浏览: 2018092 次
  • 来自: 武汉
社区版块
存档分类
最新评论

YII 利用Clip构建layout

 
阅读更多

Yii为我们提供了layout,我们可以把一个view嵌入设计好的layout。但随着网站设计越来越多变,越来越复杂,只是layout内一个$content 变量在载入view似乎并不能满足我们的需求。如果页面中有好几个地方需要时时变动呢?没关系, Yii还为我们提供了Clip这个方法

<?php $this->beginContent('/layouts/layout'); ?>
   <?php echo $content; ?>
<?php $this->endContent(); ?>

比如现在我们需要构建一个布局,左边是主要内容,侧边栏上有个子菜单和一段介绍文字,在每个不同的页面上,子菜单和介绍文字都是是不同的。让我们看看代码,这个应该更直接:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Clip</title>
    </head>
    <body>
        <div id="header">
            this is header and main menu here
        </div>
        <div id="main">
            <div id="left">
                <?php echo $content ?>
            </div>
 
            <div id="right">
                <div class="block">
                    <?php echo $this->clips['submenu'] ?>
                </div>
 
                <div class="block">
                    <?php echo $this->clips['desc'] ?>
                </div>
            </div>
        </div>
        <div id="footer">
            footer section
        </div>
    </body>

 在这段代码中我们建立了一个layout,div#left 这里我们载入了view,而在侧边栏div#right中调用了2个clip片段。

接下来我们看看clip中view的实现:view1

hello, this is the view 1
 
<?php $this->beginClip('submenu') ?>
here is the sub menu for view 1
<?php $this->endClip() ?>
 
<?php $this->beginClip('desc') ?>
here is the description for view 1
<?php $this->endClip() ?>

 view2

hi, view 2 is here
 
<?php $this->beginClip('submenu') ?>
here is the sub menu for view 2
<?php $this->endClip() ?>
 
<?php $this->beginClip('desc') ?>
here is the description for view 2
<?php $this->endClip() ?>

上面的两个view中,我们为submenu和desc片段都设定了不同的内容,如果是更复杂的页面我们还可以继续添加更多的clip片段。

 
分享到:
评论
1 楼 包菜兄 2013-01-26  
嘿嘿。。这个好

相关推荐

Global site tag (gtag.js) - Google Analytics