簡體   English   中英

Magento:在phtml文件中獲取一個靜態塊作為html

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

我有一個名為newest_product (帶內容)的靜態塊,我想將它作為html顯示在.phtml文件中。

我試過這段代碼:

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

但這一切都沒有顯示出來。

我使用錯誤的代碼嗎?

如果您已從管理面板創建名為“block_identifier”的CMS塊。 接下來將是用.phtml調用它們的代碼

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

在布局中(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'); ?>

不要忘記緩存清理。

我認為這有幫助。

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

如果要將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());

我認為這對你有用

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

它確實有效,但現在CMS塊中的變量不再解析了:(

當您在Magento中調用CMS-Static Block時,以下代碼將起作用。

<?php echo 

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

?>

這應該按測試的方式工作。

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

從管理面板創建名為block_identifier的新CMS塊時,可以使用以下代碼從.phtml文件中調用它:

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

然后清除緩存並重新加載瀏覽器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM