Use magento CMS block code in template files - php

This is the first time I am creating a custom tempalte for Magento. I installed a 3rd party plugin, that allows me to use this code in the cms editor:
{{block type='bannerslider/bannerslider' template='bannerslider/bannerslider.phtml'}}
Now, I want to use this on a page in my template. What php code should I use to get this to work ?
I tried
<?php echo $this->getChildHtml('bannerslider/bannerslider') ?>
But that displays nothing.
Thank you!

You have to declare your block inside layout file of your custom template (usually, local.xml) under some handle (like default or catalog_product_view).
For example:
<block type="bannerslider/bannerslider" name="banner_home" template="bannerslider/bannerslider.phtml"/>
under <default> handle would let you to call <?php echo $this->getChildHtml('banner_home') ?> in any place of your template. Notice that you have to use name from layout, not block class to call it with getChildHtml.

An alternative I received from someone else:
<?php echo $this->getLayout()->createBlock('bannerslider/bannerslider')->setTemplate('bannerslider/bannerslider.phtml')->toHtml();
?>

Related

How to use shortcode in Magento phtml page

I have a short code
{{block type="ibtheme/product_list_featured" category_id="51" random_products="" template="catalog/product/list/featured.phtml"}}
which is working fine in editor from backend. How can U call the same short-code from a PHTML page ?
When I put the same code, it is printing a simple text.
phtml is php code, not cms html passed through a filter to catch the short codes (macros) and expand them out.
The contents between "{{" and "}}" must interpreted by a template engine and is only valid inside emails, CMS pages/blocks and the wysiwyg editors in the backend.
You put their equivalent into layout and call them as in the following ->
Magento Shortcode CMS block not working on product pages
In Magento CMS or Static block, if you want to add PHP code then you can just call any custom .phtml file by using following code. Like here I am including my_custom.phtml.
{{block type="core/template" name="myCustom" template="cms/my_custom.phtml"}}
This is equivalent to following layout tag:
<block type="core/template" name="myCustom" template="cms/my_custom.phtml">
Hope you find it useful.
The above answers are both incorrect if I'm reading that you would like to use shortcodes in phtml pages. I use these frequently, since we have a tremendous amount of content, and breaking them down in to phtml blocks is the easiest way for us to keep our content fresh.
Anyhow, here's the correct way to use call blocks in phtml:
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('cms/my_custom.phtml')->toHtml(); ?>
For Example, to use the block in your original answer would be
<?php echo $this->getLayout()->createBlock('ibtheme/product_list_featured')->setTemplate('catalog/product/list/featured.phtml')->toHtml(); ?>
I think this is what you're actually looking for. The code is in the CMS module in the Magento code.
<?php
// Load the cms helper
$helper = Mage::helper('cms');
// get the cms static block processor
$processor = $helper->getBlockTemplateProcessor();
// run the content with the shortcode through the filter
// in this case $item->getAnswer() contains a shortcode
$html = $processor->filter($item->getAnswer());
// print it to the page
echo $html;
?>
Remember anything is possible, just dig deeper. If in doubt, copy the Magento core code.

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

I'm using Magento.
I want display and call one phtml file as a link in another phtml file…
I have the new.phtml file on the home page. On that I put one link CHECK ALL which display all new products as category page.. It looks like category page. For that I create another phtml file named newproductpage.phtml which has same code of new.phtml. Now I try to call this newproductpage.phtml file #homepage as CHECK ALL link for that I write this code....
CHECK ALL
But its not working....
thnx..
you call newproductpage.phtml in any phtml file using below code
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('custom/newproductpage.phtml')->toHtml(); ?>
Use the below code for rendering your phtml file in another phtml file.
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('templatefolderpath/filename.phtml')->toHtml(); ?>
For more refinement of your code, you can use the block name and block file name instead of core/template because core/template use the core resources.
You can not call directly one phtml file to another phtml file.
But there are two way to call your phtml file either create one controller and create one action and call action from your anchor tag or create one cms page which call your phtml file.
if you create one module, so in your layout file something you can write
<modulename_controllername_controlleraction>
<reference name="content">
<block type="catalog/product_new" template="custom/newproductpage.phtml" />
</reference>
</module_controllername_controlleraction>
Or you can directly put this code in your cms page content area
{{block type="catalog/product_new" template="custom/newproductpage.phtml"}}
and in anchor tag give cms page link.
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('catalog/product/new.phtml')->toHtml(); ?>
you can also check the link.
Display .phtml page in another .phtml page
I believe you and I want to do precisely the same thing. I'm creating a modal from bootstrap and I need to call in a partial using the href attr.
So far, I think this might be possible by creating a page in the CMS and then using something like this:
<li>Home</li>
But honestly, I'm just starting out with Magento and know very little.
you can use iframe for same and load this another page content using AJAX call

Custom template or theme (header and footer) for PDP page in Magento

We're redoing our theme in Magento to use a grid system (semantic.gs), and I've been tasked with the duty setting up the PDP page to use this new grid system template. We eventually will want this template on all pages, but for now, it's been specifically requested ONLY on the PDP page. So basically I want to set a custom theme/template (including header and footer, not just columns) that I can load on certain pages. Can anyone point me in the direction of how to go about doing this? Extensive SO and google searching has come up pretty fruitless.
So far:
I created a new layout called 2columns-left-grid.phtml in app/design/frontend/[my_interface]/[my_theme]/template/page
I modified these lines:
<?php echo $this->getChildHtml('header') ?>
and
<?php echo $this->getChildHtml('footer') ?>
To be:
<?php echo $this->getChildHtml('header2') ?>
and
<?php echo $this->getChildHtml('footer2') ?>
I duplicated header.phtml and named it header2.phtml, and duplicated footer.phtml and named it footer2.html
My questions:
Will this work, and if so, what other code do I need to modify (xml files, etc)
How can I get this template to apply ONLY to the PDP page for now?
Thanks!
Hello you can override product tag
< catalog_product_view translate="label">
/app/design/frontend/base/default/layout/catalog.xml
from into your new module layout xml

BannerSlider Extension

I'm using the BannerSlider Extension and it works great but for design purposes I need it to include it in a template.
At this moment I'm using it with the CMS like this:
{{block type='bannerslider/bannerslider' template='bannerslider/bannerslider.phtml'}}
How can I get the PHP code for that single line, to add it directly to my template ?
Create cms/block and place your code
{{block type='bannerslider/bannerslider' template='bannerslider/bannerslider.phtml'}}
and then you can call anywhere in template that cms/block like this
<?php
echo $this->getLayout()->createBlock('cms/block')->setBlockId('YOUR IDENTIFIER')->toHtml()
?>

add code snippet in magento core

I'm running magento 1.4 and I'm trying to display an overlayer banner on all the pages in my magento store. In which file should I add the code snippet for the banner so that the it gets displayed on all pages?
BTW: the code snippet is actually some a short php if function + an OpenX Javascript Tag
From this search:
Go to /admin/cms_block/ and add a block. Remember the identifier.
In your code add <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('identifier')->toHtml() ?>
There you go…
For javascript to be included on every page have a look at page.xml layout <default> section. For PHP code see which blocks are included on the page and create a block after (or before) one of them

Categories