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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4668656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:55:21  来源:igfitidea点击:

Magento: get a static block as html in a phtml file

phpmagento

提问by iamjonesy

I have a static block called newest_product(with content) and I would like to display it on a .phtmlfile as html.

我有一个名为newest_product(带有内容)的静态块,我想将它显示在.phtml文件中作为html.

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?

我使用了错误的代码吗?

回答by Suman-PHP4U

If you have created CMS block named 'block_identifier' from admin panel. Then following will be code to call them in .phtml

如果您从管理面板创建了名为“block_identifier”的 CMS 块。然后以下将是在 .phtml 中调用它们的代码

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

回答by Max Pronko

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.

我认为它有帮助。

回答by Ayush Sugandhi

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

and use this link for more http://www.justwebdevelopment.com/blog/how-to-call-static-block-in-magento/

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

回答by Jeroen

If you want to load a cmsblock into your template/blockfile/model etc. You can do this as followed. This will render any variables places in the cmsblock

如果您想将 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());

回答by Kanak Vaghela

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 块中的变量不再解析:(

回答by user3057379

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();

?>

回答by Farhan Islam

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

回答by Farhan Islam

When you create a new CMS block named block_identifierfrom 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.

然后清除缓存并重新加载浏览器。