magento 中有没有一种方法可以创建一个 block 并调用其 toHtml()
方法而无需加载整个布局对象。
例如,在我的一个 ajax Controller 中,我想将某个 block 作为 json 输出发送,并且我对任何其他 block 不感兴趣,但我被迫执行以下操作:
$this->loadLayout();
$this->getLayout()->getBlock('my_block_name')->toHtml();
这会加载整个布局,这似乎是不必要的。
最佳答案
我只是这样完成的:
在我的模块的布局 XML 中:
<mymodule_ajax_action>
<block type="core/template_facade" name="root" template="path/to/template/file.phtml"/>
</mymodule_ajax_action>
通过将 block 命名为“root”,它仅用此模板文件替换了整个布局。
所以在我的 Controller 中:
public function actionAction() {
$this->loadLayout();
$this->renderLayout();
}
仅返回该 block 。
关于Magento布局: load only a single block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11601954/