php Magento 在 vi​​ew.phtml 中包含自定义 phtml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8909702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:46:35  来源:igfitidea点击:

Magento Including a CUSTOM phtml file in view.phtml

magentophp

提问by user1156108

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. But I do not properly understand Magento's XML syntax.

但是我知道我需要向 catalog.xml 添加一些东西,以便 Magento 识别标注并可以获取正确的文件。但我没有正确理解 Magento 的 XML 语法。

Could anyone assist?

有人可以帮忙吗?

回答by Zachary Schuessler

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 模板和块的使用方式不一致。

回答by eyeonu

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 的特定页面上调用 .phtml 块?

and

want to call one phtml file in another phtml file using anchor tag

想使用锚标记在另一个 phtml 文件中调用一个 phtml 文件

回答by vicch

Given the information you provided, I can only give a general solution.

鉴于您提供的信息,我只能给出一个通用的解决方案。

First you need to find the layout XML for this view.phtml. You should be looking for something like:

首先,您需要找到此 view.phtml 的布局 XML。你应该寻找类似的东西:

<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:

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

<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/templateor a subtype of it.

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

回答by Akash Raj

The answer to this question is below codes,just change "directory/acc_drop.phtml" to your file path name.

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

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