【问题标题】:Add block before content in magento page在magento页面的内容之前添加块
【发布时间】:2015-05-27 05:02:48
【问题描述】:

我需要在我的一个页面中的面包屑之后添加一个块,my-page

我尝试在管理员中创建一个名为my-block 的块,并在frontend/theme/default/template/page/2columns-left.phtml 中添加<?php echo $this->getChildHtml('my-block') ?>,但它不显示块my-block 的内容。

我也加了

<page-name>
    <reference name="root">
        <block type="core/template" name="my-block" as="my-block" before="content" after="breadcrumbs" template="page/html/my-block.phtml"/>
    </reference>
</page-name>

在local.xml文件中,但我不确定是否真的有必要。

我错过了什么吗?

谢谢

【问题讨论】:

    标签: php xml magento layout


    【解决方案1】:

    必须以不同的方式调用 Cms 块来回显其内容。

    在 breadcrumbs.phtml 下面的所有页面上呈现块:

    app\design\frontend\base\default\layout\page.xml

    在面包屑后添加:

    在 Layout.xml 中

    <block type="cms/block" name="block_name">
        <action method="setBlockId"><block_id>my-block</block_id></action>
    </block>
    

    然后你必须在 3columns.phtml 中回显它:

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

    可能更容易在每页的基础上进行。

    打开模板提示,找到一个你想附加你的 cms 内容的块,并在其中放置以下内容:

    在模板文件中:

     // Insert the block into the page.
    $sBlockId = 'my-block';
    $oBlock = Mage::getModel( 'cms/block' );
    $oBlock->setStoreId( Mage::app()->getStore()->getId() );
    $oBlock->load( $sBlockId, 'identifier' );
    $oCmsHelper = Mage::helper( 'cms' );
    $oProcessor = $oCmsHelper->getPageTemplateProcessor();
    $sHtml = $oProcessor->filter( $oBlock->getContent() );
    echo $sHtml;
    

    或者:

    echo Mage::app()->getLayout()->createBlock( 'cms/block' )->setBlockId( 'my-block' )->toHtml();
    

    理想情况下,您可能希望创建一个模板,然后通过 layout.xml 附加并在该模板内回显您的 cms 块。

    创建一个模板,在任何你想要的地方呈现你的 cms 块:

    app\etc\modules\Spirit_Cms.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Spirit_Cms>
                <active>true</active>
                <codePool>local</codePool>
            </Spirit_Cms>
        </modules>
    </config>
    

    app\code\local\Spirit\Cms\etc\config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Spirit_Cms>
                <version>0.0.1</version>
            </Spirit_Cms>
        </modules>
        <frontend>
            <layout>
                <updates>
                    <spirit_cms>
                        <file>custom.xml</file>
                    </spirit_cms>
                </updates>
            </layout>
        </frontend>
    </config>
    

    app\design\frontend\rwd\default\layout\custom.xml

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <default>
            <reference name="content">
                <block type="core/template" name="custom" template="custom.phtml" output="toHtml" />
            </reference>
        </default>
    </layout>
    

    app\design\frontend\rwd\default\template\custom.phtml

    <?php echo Mage::app()->getLayout()->createBlock( 'cms/block' )->setBlockId( 'custom' )->toHtml(); ?>
    

    在指定位置添加内容更灵活/更简洁。

    【讨论】:

    • 一个秒人回答了一个很长的问题。不久将编辑一个示例。
    • 我在上面添加了一个完整的方法来添加 cms 块
    猜你喜欢
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2014-07-21
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多