문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top