【问题标题】:How to call Magento block in phtml template?如何在 phtml 模板中调用 Magento 块?
【发布时间】:2012-05-18 06:17:53
【问题描述】:

我需要在页脚中显示更多链接。我在 magento admin 中将这些链接创建为静态块(id = sample_links)。

然后我添加了以下代码 page.xml 文件

<reference name="foot_lnk">  
<block type="cms/block" name="sample_block" before="-">
      <action method="setBlockId"><block_id>sample_links</block_id></action>
    </block>
</reference>

我在 footer.phtml 中将其称为,

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

但它不显示 CMS 静态块内容。有什么问题?

【问题讨论】:

    标签: php magento content-management-system block


    【解决方案1】:
    $this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() 
    

    【讨论】:

    • 是的,我在上面使用过它正在工作。但我需要像上面那样使用 XML 显示。
    【解决方案2】:

    引用是之前定义的你希望你的块在里面的块,例如:

    <reference name="footer">
      <block type="cms/block" name="sample_links">
        <action method="setBlockId"><block_id>sample_links</block_id></action>
      </block>
    </reference>
    

    然后

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

    【讨论】:

    • 它的工作谢谢。我已经在 标签中有默认页脚链接。我在其中添加了块代码 sample_links
    【解决方案3】:

    你可以像这样调用静态块:

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

    并调用如下块:

    <?php echo $this->getLayout()->createBlock('sidebar/left')->setTemplate('bannerslider/left.phtml')->tohtml(); ?>
    

    访问 magevn.com 以查看更多在 magento 中使用块的用例。

    【讨论】:

    • 更完整的答案,感谢您提供的信息,使谷歌搜索更加完整:)
    【解决方案4】:

    如果您不想打扰 XML,就像 swapnesh 的回答一样,我只是让那里的 php 新手更清楚(像我一样)

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

    your_identifier 是您在 CMS > Blocks > Create New Block 中创建块时决定使用的代码,第二行称为“标识符”

    【讨论】:

    • 那么,您应该编辑他的答案以增加精度,而不是发布几乎相同的答案。
    【解决方案5】:

    将您的参考名称更改为 footer

    喜欢

    <reference name="footer">  
    

    然后它会工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      相关资源
      最近更新 更多