首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

在代码中动态更替页面布局

2012-12-24 
在代码中动态更换页面布局假设要在category列表页根据传入category的条件(如id10或者不大于10)动态改变其

在代码中动态更换页面布局
假设要在category列表页根据传入category的条件(如id>10或者不大于10)动态改变其布局。方法是:

在Block类的回调方法_prepareLayout中调用类似的代码

protected function _prepareLayout(){   parent::_prepareLayout();   $cid = $this->getCurrentCategory()->getId();   if($cid>10) {      $this->getLayout()->getBlock('root')->setTemplate('page/2rows-1column.phtml');   } else {      $this->getLayout()->getBlock('root')->setTemplate('page/another.phtml');   }}

可以用函数
$this->getLayout()->getBlock('root')->setAttribute('template','page/2rows-1column.phtml');
替换
$this->getLayout()->getBlock('root')->setTemplate('page/2rows-1column.phtml');

热点排行