简体   繁体   English

CMS 页面添加块 magento

[英]CMS page add block magento

I have in CMS->page "home page" file.我在 CMS-> 页面“主页”文件中。 In content i am writing line like this:在内容中我这样写:

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

I want to render in content file newfile.phtml.我想在内容文件 newfile.phtml 中呈现。 What i am doing wrong?我做错了什么?

My new file is under: app\design\frontend\default\themeas\template\myfolder\newfile.phtml我的新文件位于:app\design\frontend\default\themeas\template\myfolder\newfile.phtml

You need to give your block a name.你需要给你的块一个名字。 That's how Magento will reference the block.这就是 Magento 将如何引用该块。 Also, your block type must be valid in order for the block to render.此外,您的块类型必须有效才能呈现块。 For default blocks try using type="core/template"对于默认块尝试使用 type="core/template"

Your new code should look like this:您的新代码应如下所示:

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

Another note about the type attribute, its not actually a directory/file structure, rather, it's a URI that is mapped with the Magento autoloader.关于 type 属性的另一个注意事项,它实际上不是一个目录/文件结构,而是一个映射到 Magento 自动加载器的 URI。 "Core" relates back to the Mage_Core_Block_Core class (under the app/code/core/Mage/Core directory) and then the info after the slash relates to the folders inside of that directory. “Core”与 Mage_Core_Block_Core class(在 app/code/core/Mage/Core 目录下)相关,然后斜线后的信息与该目录内的文件夹相关。 So type="core/template" resolves to this class Mage_Core_Block_Core_Template which is located at app/code/core/Mage/Core/Block/Template.php.所以 type="core/template" 解析为位于 app/code/core/Mage/Core/Block/Template.php 的 class Mage_Core_Block_Core_Template。 All the type attribute is doing is telling Magento which methods you need to load inside of your block. type 属性所做的就是告诉 Magento 您需要在块中加载哪些方法。

A couple other block types you can try are:您可以尝试的其他几种块类型是:

For Product Lists: catalog/product_list对于产品列表: catalog/product_list

For Text Lists (blocks that automatically render out child blocks): core/text_list对于文本列表(自动呈现出子块的块): core/text_list

For Category Blocks: catalog/category_view对于类别块: catalog/category_view

There are plenty more, a good way to find new ones is to look at a block that does a similar action to what you are trying to do, and find where it is defined in the XML.还有很多,找到新块的一个好方法是查看与您尝试执行的操作类似的块,并找到它在 XML 中的定义位置。

If you want to pass variables to the block, you can do something like:如果你想将变量传递给块,你可以这样做:

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

Since Magento 1.9.2.2, or equvivalent patch you also need to grant permissions to the new block.自 Magento 1.9.2.2 或等效补丁以来,您还需要授予对新块的权限。 You do this in the backend: System |您在后端执行此操作:System | permissions |权限 | blocks积木

Ie if you wish to show:即如果你想显示:

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

Add your block name "catalog/product_bestseller" and set the status to "allowed"添加您的块名称“catalog/product_bestseller”并将状态设置为“允许”

I'd like to offer an alternative:我想提供一个替代方案:

The above answers work fine, however it's my personal preference to not insert blocks in the content of a CMS page as clients can (and have) deleted this crucial line when trying to edit text and content using the WYSIWYG.上面的答案工作正常,但我个人偏好不在 CMS 页面的内容中插入块,因为客户在尝试使用所见即所得编辑文本和内容时可以(并且已经)删除这一关键行。

You could add the following in the the Layout > Layout update XML section of a CMS page:您可以在 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>

This way, clients are less likely to edit this tab!这样,客户就不太可能编辑此选项卡!

Hope this helps anyone else with this issue!希望这可以帮助其他人解决这个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM