简体   繁体   中英

CMS page add block magento

I have in CMS->page "home page" file. 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. What i am doing wrong?

My new file is under: 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. Also, your block type must be valid in order for the block to render. For default blocks try using 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. "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. 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. All the type attribute is doing is telling Magento which methods you need to load inside of your block.

A couple other block types you can try are:

For Product Lists: catalog/product_list

For Text Lists (blocks that automatically render out child blocks): core/text_list

For Category Blocks: 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.

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. You do this in the backend: 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"

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.

You could add the following in the the Layout > Layout update XML section of a CMS page:

<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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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