将自定义的phtml文件包含在view.phtml中的Magento中

6

我正在尝试想出如何创建自定义phtml文件,以便在view.phtml上包含它(最终可以从任何默认的Magento phtml文件中调用)。

我已经创建了一个名为productbadges.phtml的单独phtml文件,并将需要的内容放入其中。

This will be pulled through as the last item in

I understand the callout usually is

<?php echo $this->getChildHtml('phtmlfilename') ?>

However I know I need to do add something to catalog.xml so Magento recognizes the callout and can source the correct file. But I do not properly understand Magento's XML syntax.

Could anyone assist?

4个回答

6

vicch的回答是正确的方法。

然而,还有一种可选的方法:

$block = $this->getLayout()->createBlock(
      'Mage_Core_Block_Template',
      'choose_a_block_name',
       array('template' => 'folder/myphtmlfile.phtml')
 );

我发布这篇文章是为了普及知识,但这并不是正确的做法,因为它与Magento模板和块的使用方式不一致。


6

5
鉴于您提供的信息,我只能提供一个普遍的解决方案。
首先,您需要找到此 view.phtml 的布局 XML。您应该寻找类似以下内容的东西:
<block type="..." name="..." ... template="../view.phtml">

要在包装块的直接下方添加新模板的声明,应该是:

<block type="..." name="..." ... template="../view.phtml">    
    <block type="..." name="phtmlfilename" template="../phtmlfilename.phtml"/>
    ...
</block>

还可以在其他地方引用外部块:

<reference name="[name_of_view.phtml_block]">
    <block type="..." name="phtmlfilename" template="../phtmlfilename.phtml"/>
</reference>

新模板的类型是类名,应该是core/template或其子类型。

0

这个问题的答案在下面的代码中,只需将 "directory/acc_drop.phtml" 更改为您的文件路径名称即可。

    <?php echo $this->getLayout()->createBlock('core/template')->setTemplate('directory/acc_drop.phtml')->toHtml(); ?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接