我搜索了网络,找不到如何做到这一点。

我想在CMS页面上添加我在管理员中创建的静态块。这使用了这两条行:

{{block type="cms/block" block_id="my_block_id"}}
{{block type="cms/block" block_id="my_block_id" template="cms/content.phtml"}}

现在我想使用自定义模板,即:

{{block type="cms/block" block_id="my_block_id" template="modulename/custom-template.phtml"}}

当我将其放入这样的CMS页面时,显示了块,但是“模板” -TAG被完全忽略 - cms/content.phtml 用来。

我尝试了什么
我尝试扩展class mage_block_cms_block并添加 setTemplate($this->getTemplate());_toHtml()-功能。效果与上述相同 - cms/content.phtml 用来。

我尝试扩展class mage_core_block_template;当然,我可以在此处设置一个模板,但是要获得静态块有问题。我找不到如何通过块ID获取块。

这个问题是关于 /无关的
我知道如何使用PHP做到这一点。
我知道如何使用XML文件做到这一点。
对于这个问题,可以在后端进行管理,这对于这个问题至关重要。

我运行Magento CE 1.7.0.0。

感谢您的时间!

有帮助吗?

解决方案

您无法更改静态块的模板,因为静态块没有模板。看一下方法: Mage_Cms_Block_Block::_toHtml(). 。这 _toHtml() 方法用于渲染任何块对象,在CMS块的情况下,它仅呈现块的内容。

如果要在某些标记中包装任何CMS块的内容,则可以尝试以下操作:

{{block type="core/template" template="custom/block.phtml" block_id="some_block_id"}}

在文件中 custom/block.phtml 做这个:

<?php
$block = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($this->getBlockId()); //retrieve the cms block with the id set on this layout block
$html = $block->toHtml();//in this var you have the content of the cms block 
?>
<?php if ($html) : //this is needed to avoid additional markup if the cms block is empty?>
<div class="some-class">
    <div class="some-other-class">
        <?php echo $html;//actuall cms block?>
    </div>
</div>
<?php endif;?>

我希望这是您需要的。

其他提示

你尝试过吗 cms/widget_block?这个块从 Mage_Core_Model_Template, ,因此有可能做您要做的事情。

许可以下: CC-BY-SA归因
scroll top