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(); ?>
Related
I have created a custom module that attaches some information to each product. Now, I want to show the custom information on Catalog page under product name.
Here is snapshot to show you what I am trying to do.
Here is my layout xml
<?xml version="1.0"?>
<!--
/**
* Custom module
*/
-->
<layout>
<catalog_category_default translate="label">
<reference name="name.after">
<block type="catalog/product_list" name="custom_content" template="custommodule/default.phtml"/>
</reference>
</catalog_category_default>
</layout>
But this is not working. Is it possible to render custom content under product name without modifying any .phtml files?
Many Thanks!
I'm aware that this is a quick and dirty hack that more diligent magento devs will sneer at, but it will give you the result you want and is easy for you to impliment
The 'correct' solution probably involves writing a custom block and calling it in the foreach loop of your app/design/frontend/YourPackage/YourTheme/template/product/list.phtml - however, I can offer you a quick and dirty hackaround that shouldn't tax you too much. You can do it very easily by abusing the product attributes system.
First create a new product attribute in the attribute set used by your product.
We'll use this to store the custom content we want to inject between your product name and price. For the sake of this example we'll call it customcontent. Make sure you set the attribute's Visible on "Product View Page on Front-end" value to 'No'.
Edit your product and set this new attribute equal to your desired custom content - it's fine you use html tags in here too. By default, product attributes have (for some reason) an arbitrary limit of 30 characters but if you need it to hold a longer string you can easily change this by editing app/code/mage/eav/model/entity/Attribute.php and changing the value of ATTRIBUTE_CODE_MAX_LENGTH.
Then in your app/design/frontend/YourPackage/YourTheme/template/product/list.phtml and find the following line of code somewhere around line 49
<?php foreach ($_productCollection as $_product): ?>
directly after it insert the following:
<?php $_customcontent =$_product->getResource()->getAttribute('customcontent')->getFrontend()->getValue($_product);?>
You've now stored the string from your products customcontent attribute in a variable called $_customcontent
So if you find the following line in your list.phtml
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></h2>
you can easily add a new paragraph or heading beneath it and populate it with your custom content using
<?php echo $_customcontent ?>
I know this probably represents a deeply unorthodox solution, but it does work and can be acheived very quickly, and without requiring you to know anything about writing your own custom blocks.
Can you try :
<catalog_categoryd_default>
<reference name="content">
<block type="catalog/product_list" name="custom_content" template="custommodule/default.phtml" after="nameofTheNameBlock"/>
</reference>
</catalog_categoryd_default>
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').
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.
i put the code in my theme header.phtml
echo $this->getCurrencyCount() and $this->getCurrentCurrencyCode() both of them are no any output. but i have set 4 currencies for the site.
but in the same file, $this->getWelcome() have the right output.why?
when i echo Mage::app()->getStore()->getCurrentCurrencyCode(); in the file ,it has a value. thank you.
if i want to output the currency switcher. how do i do?
These function are from the Mage_Directory_Block_Currency model, not the Mage_Page_Block_Html_Header one.
You might look at the directory/currency.phtml file to handle this kind of task
IF you want to determine the best position in the page thanks to the header.phtml file, just define this block as a child of the header one.
In your theme layouts, in directory.xml, in the <default> area add this :
<reference name="header">
<block type="directory/currency" name="header_currency" before="catalog.leftnav" template="directory/currency.phtml"/>
</reference>
Then in the header.phtml file just add echo $this->getChildHtml('currency'); where you need it to show.
If you just need these variables without showing the currency block use in your header.phtml this code
$currency_block = new Mage_Directory_Block_Currency;
$currency_block->getCurrentCurrencyCode();
The last part is provided as is and is untested.
I am trying to customize a theme using only local.xml whenever possible. I want to add a static block to the header without modifying header.phtml. This code works fine for the content area, but not for the header:
<default>
<reference name="content">
<block type="cms/block" name="how-it-works-button">
<action method="setBlockId"><block_id>how-it-works</block_id></action>
</block>
</reference>
</default>
Anybody know why? I thought that all I would need is to change “content” to “header”, but nothing shows up when I do.
Thanks for your help!
The content block is a special block known as a core/text_list block (PHP class Mage_Core_Block_Text_List). These blocks will automatically render out any child blocks that are added to them.
The header block, on the other hand, is a page/html_header block (PHP class Mage_Page_Block_Html_Header). This block class inherits from Mage_Core_Block_Template, making it a core/template block. Template blocks will only render sub-blocks if their corresponding phtml template requests the block. So, by adding your block to the header, you're only doing half the work you need to. You'll need to create a custom phtml template.
The simplest way to do this (post 1.4.1.1 is to, in your own theme, create a file at
template/page/html/header.phtml
And then at the end of this file add
<?php echo $this->getChildHtml('how-it-works-button'); ?>
Assuming you've added a block to header block via layout xml, this should render your template.
Please Try this
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('how-it-works')->toHtml() ?>
And this code in header.phtml
add output="toHtml" in the block tag. I think it is only that.