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

发布于 2024-10-11 18:36:38 字数 293 浏览 3 评论 0原文

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

我已经尝试过这段代码:

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

但是没有显示任何内容。

我使用了错误的代码吗?

I have a static block called newest_product (with content) and I would like to display it on a .phtml file as 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

梦情居士 2024-10-18 18:36:39

这应该按测试工作。

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

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;
?>
昨迟人 2024-10-18 18:36:39

当您从管理面板创建名为 block_identifier 的新 CMS 块时,您可以使用以下代码从 .phtml 文件中调用它:

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

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

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:

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

Then clear the cache and reload your browser.

送舟行 2024-10-18 18:36:38

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

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

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

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); 
?> 
奶气 2024-10-18 18:36:38

在布局 (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>

在 phtml 模板中:

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

不要忘记缓存清理。

我认为这有帮助。

In the layout (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:

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

Don't forget about cache cleaning.

I think it help.

心舞飞扬 2024-10-18 18:36:38
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() ?>

并使用此链接了解更多信息
http://www.justwebdevelopment.com/blog/如何在magento中调用静态块/

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

人海汹涌 2024-10-18 18:36:38

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

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

$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());
深白境迁sunset 2024-10-18 18:36:38

我认为这对你有用

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

它确实有效,但现在 CMS 块中的变量不再解析:(

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 :(

才能让你更想念 2024-10-18 18:36:38

当您在 Magento 中调用 CMS-Static Block 时,以下代码将起作用。

<?php echo 

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

?>

Following code will work when you Call CMS-Static Block in Magento.

<?php echo 

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文