简体   繁体   English

Magento在view.phtml中包含一个CUSTOM phtml文件

[英]Magento Including a CUSTOM phtml file in view.phtml

I am trying to work out how to create custom phtml files to include on view.phtml (and ultimately to be called from any default Magento phtml file). 我正在尝试研究如何创建自定义的phtml文件以包含在view.phtml上(并最终从任何默认的Magento phtml文件中调用)。

I have created a seperate phtml file with the content I want in it called productbadges.phtml 我创建了一个单独的phtml文件,其中包含我想要的内容,名为productbadges.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. 但是我知道我需要向catalog.xml添加一些内容,以便Magento识别出标注并可以获取正确的文件。 But I do not properly understand Magento's XML syntax. 但我没有正确理解Magento的XML语法。

Could anyone assist? 有人可以帮忙吗?

you can use 您可以使用

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

see also here: 另见:

How do i call .phtml block at specfic page in magento? 如何在magento的specfic页面调用.phtml块?

and

want to call one phtml file in another phtml file using anchor tag 想要使用锚标记在另一个phtml文件中调用一个phtml文件

vicch's response is the correct way of doing it. vicch的回应是正确的做法。

However, it's also helpful to know that there is an alternate method: 但是,知道有另一种方法也很有帮助:

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

I am posting this for general knowledge. 我发布这个是为了一般知识。 This is not the accepted way of doing this, since it is not consistent with how Magento templates and blocks are used. 这不是可接受的方式,因为它与Magento模板和块的使用方式不一致。

Given the information you provided, I can only give a general solution. 鉴于您提供的信息,我只能提供一般解决方案。

First you need to find the layout XML for this view.phtml. 首先,您需要找到此view.phtml的布局XML。 You should be looking for something like: 你应该寻找像这样的东西:

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

To add the declaration of the new template directly under the wrapping block, it should be: 要在包装块下直接添加新模板的声明,它应该是:

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

It is also possible to reference the outter block somewhere else: 也可以在其他地方引用outter块:

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

Type of the new template is the class name, which should be core/template or a subtype of it. 新模板的类型是类名,它应该是core/template或其子类型。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM