user1156108
user1156108

Reputation: 61

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?

Upvotes: 6

Views: 23206

Answers (4)

Akash Raj
Akash Raj

Reputation: 21

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

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

Upvotes: 0

eyeonu
eyeonu

Reputation: 211

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

Upvotes: 6

Zachary Schuessler
Zachary Schuessler

Reputation: 3682

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.

Upvotes: 6

vicch
vicch

Reputation: 706

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.

Upvotes: 5

Related Questions