简体   繁体   English

Magento:在phtml文件中获取一个静态块作为html

[英]Magento: get a static block as html in a phtml file

I have a static block called newest_product (with content) and I would like to display it on a .phtml file as html . 我有一个名为newest_product (带内容)的静态块,我想将它作为html显示在.phtml文件中。

I've tried this code: 我试过这段代码:

echo $this->getLayout()->createBlock('cms/block')->setBlockId('newest_product')->toHtml(); 

But this nothing is being displayed. 但这一切都没有显示出来。

Am I using the wrong code? 我使用错误的代码吗?

If you have created CMS block named 'block_identifier' from admin panel. 如果您已从管理面板创建名为“block_identifier”的CMS块。 Then following will be code to call them in .phtml 接下来将是用.phtml调用它们的代码

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); 
?> 

In the layout (app/design/frontend/your_theme/layout/default.xml): 在布局中(app / design / frontend / your_theme / layout / default.xml):

<default>
    <cms_page> <!-- need to be redefined for your needs -->
        <reference name="content">
            <block type="cms/block" name="cms_newest_product" as="cms_newest_product">
                <action method="setBlockId"><block_id>newest_product</block_id></action>
            </block>
        </reference>
    </cms_page>
</default>

In your phtml template: 在你的phtml模板中:

<?php echo $this->getChildHtml('newest_product'); ?>

Don't forget about cache cleaning. 不要忘记缓存清理。

I think it help. 我认为这有帮助。

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() ?>

并使用此链接获取更多信息http://www.justwebdevelopment.com/blog/how-to-call-static-block-in-magento/

If you want to load a cmsblock into your template/blockfile/model etc. You can do this as followed. 如果要将cmsblock加载到模板/块文件/模型等中,可以按照以下步骤操作。 This will render any variables places in the cmsblock 这将使任何变量位于cmsblock中

$block  = Mage::getModel('cms/block')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load('identifier');

$var = array('variable' => 'value', 'other_variable' => 'other value');
/* This will be {{var variable}} and {{var other_variable}} in your CMS block */

$filterModel = Mage::getModel('cms/template_filter');
$filterModel->setVariables($var);

echo $filterModel->filter($block->getContent());

I think this will work for you 我认为这对你有用

$block = Mage::getModel('cms/block')->setStoreId(Mage::app()->getStore()->getId())->load('newest_product');
echo $block->getTitle();
echo $block->getContent();

It does work but now the variables in CMS block are not parsing anymore :( 它确实有效,但现在CMS块中的变量不再解析了:(

Following code will work when you Call CMS-Static Block in Magento. 当您在Magento中调用CMS-Static Block时,以下代码将起作用。

<?php echo 

$this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml();

?>

This should work as tested. 这应该按测试的方式工作。

<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="cms/widget_page_link" template="cms/widget/link/link_block.phtml" page_id="2"}}');
echo $_widget;
?>

When you create a new CMS block named block_identifier from the admin panel you can use the following code to call it from your .phtml file: 从管理面板创建名为block_identifier的新CMS块时,可以使用以下代码从.phtml文件中调用它:

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); 
?> 

Then clear the cache and reload your browser. 然后清除缓存并重新加载浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM