在静态页面中引用一个块
-
16-10-2019 - |
题
我有一个静态页面,我称之为动态块:
{{block type="custommodule/search" name="custommodule_search" template="custommodule/search.phtml" }}
在我的自定义模块的布局文件中,我添加了我的cuncommodule_search块的两个孩子:
<layout>
<default module="custommodule">
<reference name="custommodule_search">
<block type="custommodule/search_bysize" name="search_by_size" template="custommodule/search/by-size.phtml" />
<block type="custommodule/search_byvehicle" name="search_by_vehicle" template="custommodule/search/by-vehicle.phtml" />
</reference>
</default>
我只想用这两个块来调用这两个块:
$this->getChildHtml('search_by_size')
或这个:
$this->getChildHtml('search_by_vehicle')
主动力块模板中的任何位置。
问题在于,仅当引用不是“ cuncommodule”而是“内容”时才显示它们,而是在“内容”块的开头而不是我想要的地方显示的。
解决方案
由 {{block}}
无法从布局XML文件引用指令。
那是因为它们是在处理过程中实例化的 renderLayout()
步骤,调用每个块 toHtml()
方法。
在此之前,在执行之前处理了布局XML loadLayout()
步。
要解决这个问题,请在布局XML中声明没有父母块的孩子块
<layout>
<default>
<block type="custommodule/search_bysize" name="search_by_size" template="custommodule/search/by-size.phtml" />
<block type="custommodule/search_byvehicle" name="search_by_vehicle" template="custommodule/search/by-vehicle.phtml" />
</default>
</layout>
然后,在你的 custommodule/search
块模板使用 getBlockHtml()
代替 getChildHtml()
.
<?php echo $this->getBlockHtml('search_by_size') ?>
<?php echo $this->getBlockHtml('search_by_vehicle') ?>
其他提示
在自定义块的模板文件中 custommodule/search.phtml
使用以下内容显示子块并避免布局更改:
<?php
echo Mage::app()->getLayout()->createBlock('custommodule/search_bysize')->setTemplate('custommodule/search/by-size.phtml')->toHtml();
echo Mage::app()->getLayout()->createBlock('custommodule/search_byvehicle')->setTemplate('custommodule/search/by-vehicle.phtml')->toHtml();
?>