Magento createBlock 方法不起作用,显示静态块数据

发布于 2024-09-18 22:37:54 字数 652 浏览 3 评论 0原文

好的,我已经在我的 CMS 区域中创建了静态块,并且我尝试将它们输出到我构建的自定义主页模板中。

我能找到的每个文档都说按如下方式输出块

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

这对我不起作用,所以我尝试了另一种方法。 --

       <?php $block = Mage::getSingleton('core/layout')->createBlock('cms/block')->setBlockId('my-block-identifier');
        echo $block->toHtml();

所有引用此内容的站点都告诉我使用实际的块标识符来获取块。因此,我决定在 cms_block 表中手动查找 block_id,看看使用 block_id 数字代替文字 my-block-identifier 名称是否有效 - 确实如此。所以我很困惑...谁能告诉我如何通过实际标识符获取块,或者通过标识符查找块ID,以便我可以通过块名称获取块?

非常感谢任何帮助。

Ok so Ive created static blocks in my CMS area, and Im trying to output them inside of a custom homepage template Ive built.

Every document I can find says to output the block as follows

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

That didnt work for me, so then I tried another way. --

       <?php $block = Mage::getSingleton('core/layout')->createBlock('cms/block')->setBlockId('my-block-identifier');
        echo $block->toHtml();

All the sites referencing this tell me to use the actual blocks identifier to get the block. So then I decide to manually lookup the block_id in my cms_block table and see if using the block_id number in place of the literal my-block-identifier name will work - and it did. So I am confused... Can anyone tell me how I can get the block by the actual identifier, or look up the blocks id by the identifier so that I can grab the block by block name?

Any help much appreciated.

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

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

发布评论

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

评论(2

丢了幸福的猪 2024-09-25 22:37:54

查看 cms/block 块源代码,那些教程误导了您,或者您误解了它们。

#File: app/code/core/Mage/Cms/Block/Block.php
class Mage_Cms_Block_Block extends Mage_Core_Block_Abstract
{
    protected function _toHtml()
    {
        if (!$this->_beforeToHtml()) {
            return '';
        }
        $html = '';
        if ($blockId = $this->getBlockId()) {
            $block = Mage::getModel('cms/block')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->load($blockId);
            if (!$block->getIsActive()) {
                $html = '';
            } else {
                $content = $block->getContent();

                $processor = Mage::getModel('core/email_template_filter');
                $html = $processor->filter($content);
            }
        }
        return $html;
    }
}

块内容始终通过 ->load($blockId); 加载 - 带一个参数的 load 始终意味着通过数据库 ID 加载。

因此,由于块中没有内置支持的方法,您需要查找块 ID。

$model = Mage::getModel('cms/block')->getCollection()
->addFieldToFilter('identifier','footer_links')
->getFirstItem();

var_dump($model->getBlockId()); 

Looking at the cms/block block source, those tutorials have mislead you, or you misinterpreted them.

#File: app/code/core/Mage/Cms/Block/Block.php
class Mage_Cms_Block_Block extends Mage_Core_Block_Abstract
{
    protected function _toHtml()
    {
        if (!$this->_beforeToHtml()) {
            return '';
        }
        $html = '';
        if ($blockId = $this->getBlockId()) {
            $block = Mage::getModel('cms/block')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->load($blockId);
            if (!$block->getIsActive()) {
                $html = '';
            } else {
                $content = $block->getContent();

                $processor = Mage::getModel('core/email_template_filter');
                $html = $processor->filter($content);
            }
        }
        return $html;
    }
}

The block content is always loaded with ->load($blockId); -- load with one parameter always means loding by a database ID.

So, with no supported way of doing this built into the block, you'll need to look up the block ID.

$model = Mage::getModel('cms/block')->getCollection()
->addFieldToFilter('identifier','footer_links')
->getFirstItem();

var_dump($model->getBlockId()); 
ペ泪落弦音 2024-09-25 22:37:54

在管理中,当您编辑静态块的内容时,您将看到一个名为“标识符”的字段,从顶部数第二个。复制该字段的值,并将其插入到您的代码中。因此,如果您的块在管理员中称为 contact-info,那么您的代码将如下所示:

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

管理员中标识符文本框中的值也将被保存到 cms_block 中代码>表,正如你所计算的那样。

哈特哈,
京东

In the admin, when you are editing the contents of the static block, you will see a field called Identifier second from the top. Copy the value of that field, and insert it into your code. So if your Block is called contact-info in the admin, then your code will look like:

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

The value in that Identifier textbox in the admin is also what will be saved into the cms_block table, as you're worked out.

HTH,
JD

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