简体   繁体   中英

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).

I have created a seperate phtml file with the content I want in it called 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.

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?

and

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

vicch's response is the correct way of doing it.

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.

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:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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