【问题标题】:CMS page add block magentoCMS页面添加块magento
【发布时间】:2012-04-22 20:49:44
【问题描述】:

我在 CMS->page“主页”文件中有。在内容中,我正在写这样的一行:

{{block type="myfolder/newfile" template="myfolder/newfile.phtml"}}

我想在内容文件 newfile.phtml 中呈现。我做错了什么?

我的新文件位于:app\design\frontend\default\themeas\template\myfolder\newfile.phtml

【问题讨论】:

    标签: magento block


    【解决方案1】:

    您需要为您的区块命名。这就是 Magento 引用该块的方式。此外,您的块类型必须有效才能渲染块。对于默认块,请尝试使用 type="core/template"

    您的新代码应如下所示:

    {{block type="core/template" name="my.block.name" template="myfolder/newfile.phtml"}}
    

    关于 type 属性的另一个注意事项,它实际上不是目录/文件结构,而是使用 Magento 自动加载器映射的 URI。 “核心”与 Mage_Core_Block_Core 类(在 app/code/core/Mage/Core 目录下)相关,然后斜线后面的信息与该目录内的文件夹相关。因此 type="core/template" 解析为位于 app/code/core/Mage/Core/Block/Template.php 的此类 Mage_Core_Block_Core_Template。 type 属性所做的只是告诉 Magento 您需要在块中加载哪些方法。

    您可以尝试的其他几种块类型是:

    对于产品列表:catalog/product_list

    对于文本列表(自动呈现子块的块):core/text_list

    对于类别块:catalog/category_view

    还有很多,找到新的一个好方法是查看一个与您尝试执行的操作类似的块,并找到它在 XML 中的定义位置。

    【讨论】:

    • 在我的一个站点中,直到版本 1.9.2.3 之前它都没有名称,它是 {{block type="catalog/product_list" category_id="8" template="catalog/product/featured.phtml"}}
    • 别忘了给块权限,否则它不会显示(magento 1.9+版)系统->权限->块
    【解决方案2】:

    如果你想将变量传递给块,你可以这样做:

    {{block type="core/template" name="my.block.name" myvariable="5" template="myfolder/newfile.phtml"}}
    

    【讨论】:

      【解决方案3】:

      从 Magento 1.9.2.2 或等效补丁开始,您还需要授予新块的权限。您在后端执行此操作: 系统 |权限 |块

      即如果你想显示:

      {{block type="catalog/product_bestseller" name="krillo.bestseller" template="catalog/product/bestseller.phtml"}}
      

      添加您的区块名称“catalog/product_bestseller”并将状态设置为“允许”

      【讨论】:

        【解决方案4】:

        我想提供一个替代方案:

        上述答案工作正常,但我个人倾向于不在 CMS 页面的内容中插入块,因为在尝试使用 WYSIWYG 编辑文本和内容时,客户可以(并且已经)删除了这一关键行。

        您可以在 CMS 页面的 Layout > Layout update XML 部分添加以下内容:

        <reference name="content">
            <block after="-" type="your/block_type" name="block.name" template="your/block/template/file.phtml"/>
            <action method="insert" ifconfig="your/block_type">
                <block>block.name</block>
            </action>
        </reference>
        

        这样,客户不太可能编辑此标签!

        希望这可以帮助其他人解决这个问题!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-11
          • 1970-01-01
          • 2012-07-11
          • 1970-01-01
          • 2012-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多