假设要在category列表页根据传入category的条件(如id>10或者不大于10)动态改变其布局。方法是:
在Block类的回调方法_prepareLayout中调用类似的代码
可以用函数
$this->getLayout()->getBlock('root')->setAttribute('template','page/2rows-1column.phtml');
替换
$this->getLayout()->getBlock('root')->setTemplate('page/2rows-1column.phtml');
在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');
本文介绍如何根据当前分类ID动态更改页面布局。通过在PrepareLayout方法中判断条件,选择不同的模板文件实现布局变化。

被折叠的 条评论
为什么被折叠?



