如何在Magento phtml页面中使用短代码

时间:2014-03-20 04:19:17

标签: php magento magento-1.8

我有一个简短的代码

{{block type="ibtheme/product_list_featured" category_id="51" random_products="" template="catalog/product/list/featured.phtml"}}

在后端的编辑器中工作正常。如何从PHTML页面调用相同的短代码?

当我输入相同的代码时,它会打印一个简单的文本。

4 个答案:

答案 0 :(得分:1)

在Magento CMS或Static块中,如果要添加PHP代码,则可以使用以下代码调用任何自定义.phtml文件。就像在这里,我包括my_custom.phtml。

{{block type="core/template" name="myCustom" template="cms/my_custom.phtml"}}

这相当于以下布局标记:

<block type="core/template" name="myCustom" template="cms/my_custom.phtml">

希望你觉得它很有用。

答案 1 :(得分:1)

phtml是php代码,不是cms html通过过滤器来捕获短代码(宏)并将它们展开。

“{{”和“}}”之间的内容必须由模板引擎解释,并且仅在电子邮件,CMS页面/块和后端的所见即所得编辑器中有效。

您将他们的等价物放入布局并按以下方式调用它们 - &gt;

Magento Shortcode CMS block not working on product pages

答案 2 :(得分:0)

如果我正在阅读你想在phtml页面中使用短代码,那么上述答案都是不正确的。我经常使用这些,因为我们有大量的内容,并将它们分解为phtml块是我们保持内容新鲜的最简单方法。

无论如何,这是在phtml中使用调用块的正确方法:

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

例如,要在原始答案中使用块

    <?php echo $this->getLayout()->createBlock('ibtheme/product_list_featured')->setTemplate('catalog/product/list/featured.phtml')->toHtml(); ?>

答案 3 :(得分:0)

我认为这是你真正想要的。代码位于Magento代码的CMS模块中。

<?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;
?>

记住一切皆有可能,只需深入挖掘。如有疑问,请复制Magento核心代码。