CMS page add block magento
Asked Answered
F

4

11

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

Fleeta answered 10/4, 2012 at 14:40 Comment(0)
O
22

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.

Oleo answered 10/4, 2012 at 16:2 Comment(2)
in one of my sites it worked without name until version 1.9.2.3 as {{block type="catalog/product_list" category_id="8" template="catalog/product/featured.phtml"}}Ale
Dont forget to give the block permission or it wont show (magento version 1.9+) system->permission->blocksRockrose
R
7

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"}}
Riff answered 2/10, 2013 at 8:11 Comment(0)
H
5

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

I.e 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"

Hohenlohe answered 21/4, 2016 at 12:26 Comment(0)
N
4

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!

Nicollenicolson answered 3/12, 2015 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.