including phtml file in phtml in magento - magento

Hi I have added the inchoo featured products but want them to show in the header so show on everypage, i tried moving the code, i tried:
echo $this->getLayout()->createBlock('Mage_Adminhtml_Block_Template', 'block-name')->setData('template', 'inchoo/block_featured_products.phtml')->toHtml()
Im kind of new to magento so i don't know
thanks
Graham

Create a CMS static block and give a identifier name to that, lets say "featured_product".
Open page.xml file from app/design/frontend/default/YOURTEMPLATE/layout/page.xml
Find the section html_header, now add the following code
<block type="cms/block" name="header_block"><action method="setBlockId"><block_id>featured_product</block_id></action></block>
Next open the app/design/frontend/default/YOURTEMPLATE/template/page/html/header.phtml file.
Find the area to design and add the following code in there :
<?php echo $this->getChildHtml('featured_product') ?>
Clean cache and test your page.

Related

New file won't integrate in Magento with getchild()

I want to make a new pop-up and I want to copy an existing login-form modal for it.
So I have done the following things:
1) create the new modal file shipping.modal.phtml and save it under app/design/frontend/sm-maxshop/default/template/checkout
2) the new modal should work for the cart.phtml, so i have add the following code into the cart.phtml from the folder app/design/frontend/sm-maxshop/default/template/checkout -
<?php echo $this->getChildHtml('checkout_shipping_modal'); ?>
3) I have edited the page.xml (because the new modal will work for different sites) in this way
<block type="checkout/template" name="checkout_shipping_modal" after="-" template="checkout/shipping.modal.phtml" />
I think now it should work, but the code of the new "shipping.modal.html" is not included in the page.
What's wrong with my code?
I think your issue With block type..
can you give more detail what is in shipping.modal.phtml file

Magento - How to create a getChildHtml block and call it in another

Currently i'm working to redesign a custom Magento template.
I'm facing the following problem.
In file called: 2columns-left.phtml i am facing code like this several times:
<?php echo $this->getChildHtml('global_messages') ?>
or
<?php echo $this->getChildHtml('global_messages') ?>
These code lines makes me think that they are calling blocks and the content in them by getting the name of .phtml file.
So i have file called slider_layred_nav.phtml and i want to call all the content in it in file 2columns-left.phtml so i tried this code:
So in file 2columns-left.phtml i put:
<?php echo $this->getChildHtml('slider_layred_nav') ?>
But it is just not displaying anything.
I assume that i have to do something with the layout but i have no idea what.
Can you help me our resolve this mystery ?
Thanks in advance!
I guess you have some problem understanding the magento layouts and how the blocks are rendered in the template files.
In simple words,getChildHtml() renders all the blocks inside that particular block i.e child blocks of that parent block.If you use getChildHtml('slider_layred_nav') than it will render the block named slider_layred_nav not the template file.
Here $this has it own meaning. It refers to the block which has template file 2columns-left.phtml.
At first you have to create a block inside the block which use the template 2columns-left.phtml
For example:
<block type="core/template" name="slider_layred_nav" template="something/slider_layred_nav.phtml"/>
After creating this block inside the block that uses the template 2columns-left.phtml you can do echo $this->getChildHtml('slider_layred_nav'); to display that block inside your 2columns-left.phtml file.
Hope i made you clear to some extent.
You are indeed missing the layout part.
If you open up page.xml (under layout folder of your theme), you can find the definition of the block global_messages like so :
<block type="core/messages" name="global_messages" as="global_messages"/>
But don't take that block as example, since it is a special one which does not have a template associated with it.
The cleaner way to make what you want is like so :
Create a local.xml file under your in app/design/frontend/your_package/your_theme/layout (nota: your_package & your_theme will vary).
Then paste this one in the new local.xml file :
<?xml version="1.0"?>
<layout version="0.1.0"><!-- everything goes in here -->
<default>
<block type="core/template" name="slider_layred_nav" template="path/to/slider_layred_nav.phtml"/>
</default>
</layout>
path/to/slider_layred_nav.phtml being the path to your phtml from app/design/frontend/your_package/your_theme/template/
slider_layred_nav in the name attribute of the block being the name passed as parameter when you are calling $this->getChildHtml('some_name').

Magento static block is not showing on my layout although I followed the instructions correctly

I want to add a static block on my layout. Therefore from the backend I add a new static block and gave an identifier for that. Then I put my block code inside the page.xml file. This is my code,
<block type="cms/block" name="templatename" template="page/html/templatename.phtml">
<action method="setBlockId"><block_id>my_id</block_id></action>
</block>
Then I put the php code inside my phtml (templatename.phtml) file to show the data. This is my code,
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_id')->toHtml();?>
Finally my phtml template file is loading in my layout.phtml file. This is that code,
<?php echo $this->getChildHtml('templatename') ?>
But my static block isn’t showing. Why is that? My Magento version is 1.8
if you want to call cms static block in your custom phtml file
just use below code to call with unique id define by you in admin cms static block section
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_id'')->toHtml()?>
that will sure display your static block in to you custom phtml files.
or you can use detail link
http://importantmagento.blogspot.in/2012/06/magento-how-to-call-static-block-on.html
i am not associate with above link for any how. it is just for knowledge purpose.
hope this will sure help you.
Thanks everyone....finally i could solve the issue.Only thing i was done,remove the block declaration from the xml file.I just write the php code on my template.Now it works fine.

Adding a new reference block to Magento

I'm having some trouble getting a custom Reference block to work in Magento.
These are the steps I have taken:
Step 1
Created a new “Reference” block in page.xml
<block type="core/text_list" name="newreference" as="newreference"></block>
Step 2
Added a reference to this block in the place I want it to appear in the page (above the footer in 1column.phtml, 2columns-left.phtml, 2columns-right.phtml, 3columns.phtml)
<?php $this->getChildHtml('newreference'); ?>
Step 3
Added a reference to catalog.xml which tells Magento I want to output a template part (specialfooter.phtml) in the ‘newreference’ Reference block on Category pages
<reference name="newreference">
<block type="core/template" name="specialfooter" template="page/html/specialfooter.phtml"></block>
</reference>
Step 4
Created ‘specialfooter.phtml’ in the page/html/ directory with a simple paragraph block to test.
And nothing happens.
The steps I have taken fit with my understanding of how Reference blocks work, but I could be wrong. I'm struggling to find any documentation, official or otherwise, or any previous SO questions which sheds any light on the subject.
I'm using Magento ver. 1.7.0.2.
Any help would be much appreciated.
Don't you have forget a echo ? :
<?php echo $this->getChildHtml('newreference'); ?>
I was having the same problem and this seems to work for me.
This block in layout/page.xml
<block type="page/html/new_newreference" name="newreference" as="newreference" template="page/html/new/newreference.phtml"/>
Can be referenced in a page eg. 1column.phtml in the template/page folder using:
<?php echo $this->getChildHtml('newreference') ?>
Note the correlation between the "type" naming and "template" path and the "name" and "as" with the getChildHtml().
Using the same principle for a product page. This block in layout/catalog.xml
<block type="catalog/product_new" name="catalogreference" as="catalogreference" template="catalog/product/new/catalogreference.html"/>
Can be referenced in template/catalog/product/view.phtml using:
<?php echo $this->getChildHtml('catalogreference'); ?>
Note both these examples are folder specific
If you want a block to use with a widget. Add this block in the appropriate reference block eg "content" or "head" in the relevant xml file eg. page.xml or catalog.xml:
<block type="core/text_list" name="mywidgetblock" as="mywidgetblock">
<label>My widget Block</label>
</block>
NB: I don't understand the "type" declaration but it works?
In the admin panel CMS/Widget/Widget instance new or existing Layout Updates/ Block Reference find "My widget Block" from the drop down.
I'm new to Magento and it took a lot of trial and error to work this out so I hope it helps someone in the same situation.

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?
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.
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
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.
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(); ?>

Resources