YII 利用Clip构建layout

简介:

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

Java代码   收藏代码
  1. <?php $this->beginContent('/layouts/layout'); ?>  
  2.         <?php echo $content; ?>  
  3. <?php $this->endContent(); ?>  

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

Java代码   收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html>  
  4.     <head>  
  5.         <title>Clip</title>  
  6.     </head>  
  7.     <body>  
  8.         <div id="header">  
  9.             this is header and main menu here  
  10.         </div>  
  11.         <div id="main">  
  12.             <div id="left">  
  13.                 <?php echo $content ?>  
  14.             </div>  
  15.    
  16.             <div id="right">  
  17.                 <div class="block">  
  18.                     <?php echo $this->clips['submenu'] ?>  
  19.                 </div>  
  20.    
  21.                 <div class="block">  
  22.                     <?php echo $this->clips['desc'] ?>  
  23.                 </div>  
  24.             </div>  
  25.         </div>  
  26.         <div id="footer">  
  27.             footer section  
  28.         </div>  
  29.     </body>  

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

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

Java代码   收藏代码
  1. hello, this is the view 1  
  2.    
  3. <?php $this->beginClip('submenu') ?>  
  4. here is the sub menu for view 1  
  5. <?php $this->endClip() ?>  
  6.    
  7. <?php $this->beginClip('desc') ?>  
  8. here is the description for view 1  
  9. <?php $this->endClip() ?>  

 view2

Java代码   收藏代码
  1. hi, view 2 is here  
  2.    
  3. <?php $this->beginClip('submenu') ?>  
  4. here is the sub menu for view 2  
  5. <?php $this->endClip() ?>  
  6.    
  7. <?php $this->beginClip('desc') ?>  
  8. here is the description for view 2  
  9. <?php $this->endClip() ?>  

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

相关文章
NLog自定义Layout Renderer
本文自定义一个NLog Layout Renderer(显示HttpClient请求的耗时)
NLog自定义Layout Renderer
|
8月前
|
XML Android开发 数据格式
Android XML 布局基础(三)LayoutParams 布局参数
Android XML 布局基础(三)LayoutParams 布局参数
131 0
|
9月前
|
编解码 Android开发
|
9月前
|
XML 前端开发 Android开发
【剪裁 widget】Flutter ClipPath
【剪裁 widget】Flutter ClipPath
91 0
【剪裁 widget】Flutter ClipPath
【布局 widget】Flutter Baseline
【布局 widget】Flutter Baseline
93 0
【布局 widget】Flutter Baseline
|
Dart 开发者
【Flutter】Image 组件 ( 内存加载 Placeholder | transparent_image 透明图像插件 )
【Flutter】Image 组件 ( 内存加载 Placeholder | transparent_image 透明图像插件 )
268 0
【Flutter】Image 组件 ( 内存加载 Placeholder | transparent_image 透明图像插件 )