Magento CMS syntax – part2

Every Magento user noticed that there is special {{magentocode}} syntax available in cms pages and static blocks. We traced a bit to find out which params are available and what exactly they do.
Magento CMS syntax – part1 is first part of this series. Please read before this.
Since the part1 article 4 new codes were introduced in newer Magento versions, so now there’s 10 of them total: skin, media, htmlescape, store, block, layout, config, customvar, protocol and widget.
They are currently handled by Mage_Widget_Model_Template_Filter processor class (which extends Mage_Cms_Model_Template_Filter which extends Mage_Core_Model_Email_Template_Filter).
5. blockDirective
Description: Prints out cms static block or any block defined by type
Example:
{{block id=”static_block_code”}} – static block example
{{block type=”core/template” template=”custom/test.phtml”}} – core/template block with defined template
Synonym:
Mage::app()->getLayout()->createBlock(‘cms/block’)->setBlockId(‘static_block_code’)->toHtml();
Mage::app()->getLayout()->createBlock(‘core/template’)->setTemplate(‘custom/test.phtml’)->toHtml();
Params:
id = static block code; if id is set type is always cms/block
type = block type, same as in layout files
Any additional parameter provided will be passed on to block object (template=”custom/test.phtml” from above example will trigger ->setTemplate(“custom/test.phtml”) on that block)
6. layoutDirective
Description: Reads and prints out layout handle defined in your layout xml files
Example: {{layout area=”frontend” handle=”my_layout_handle”}}
<sales_email_order_items> layout handle defined in sales.xml is used this way in order e-mail templates for example.
Synonym:
$layout = Mage::getModel(‘core/layout’);
$layout->getUpdate()->load(‘my_layout_handle’);
$layout->generateXml()->generateBlocks();
echo $layout->getOutput();
Params:
area = frontend or adminhtml
handle = layout handle
Any additional parameter provided will be passed to every block defined in layout
7. configDirective
Description: Prints out Magento config value. Use predefined through Insert Variable dialog in editor.
Example: {{config path=”general/store_information/address”}}
Synonym: Mage::getStoreConfig(‘general/store_information/address’);
Params:
path = config path
8. customvarDirective
Description: Prints out Custom Variable HTML Value (System->Custom Variables in admin). Can be easily added through Insert Variable dialog in editor.
Example: {{customvar code=”my_custom_var”}}
Synonym: Mage::getModel(‘core/variable’)->loadByCode(‘my_custom_var’)->getValue();
Params:
code = custom variable code
9. protocolDirective
Description: Prints out current protocol (http or https) for absolute links
Example:
{{protocol}} – current protocol http or https
{{protocol url=”inchoo.net/”}} – domain URL with current protocol
{{protocol http=”https://inchoo.net” https=”https://inchoo.net”}} – one or another
Params:
url = url to print with proper protocol prefix http(s)://; (for Magento links use {{store}} code)
http + https = prints out https value if viewing secure page, http value otherwise
store = optional store id to check secure by that store; I’m not exactly sure what’s the purpose of this
10. widgetDirective
Description: Prints out defined widget. Magento widgets automatically insert these codes, for manual custom usage it’s probably better to use block codes.
Example: {{widget type=”catalog/product_widget_new” products_count=”5″ template=”catalog/product/widget/new/content/new_list.phtml”}}
Synonym: Mage::app()->getLayout()->createBlock(‘catalog/product_widget_new’, null, array(‘products_count’=>5, ‘template’=>’catalog/product/widget/new/content/new_list.phtml’))->toHtml();
Params:
type = widget type, similar as block type
name = determine what name block should have in layout; probably needed only if more than one same widget is used on same page
Any additional parameter provided will be passed on to widget object. There is also code that accepts id param, but it currently does nothing useful.
Please be careful when copy/pasting examples from this article directly into Magento, readers have reported that quotes get messed up in the process sometimes. Please type in manually.
To be continued .. Ivan
16 comments
Big fan of X-Men I see. You use Magneto twice instead of Magento 🙂 Thanks for the article
Love for the X-Men is real, but still edited the article. Thanks for the heads up! 🙂
Is it possible to ad text (explanation) and hide it?
like // within PHP Coding?
Hi,
very usefull post! 🙂
With layout directive, what means ‘Any additional parameter provided will be passed to every block defined in layout’?
if in a cms page i do
{{layout handle=”sales_email_order_items” order=$order}}
where and how i can use $order object?
@Julian – I’m not aware of using this syntax in something like a product description, but you could easily add your own and replace them with str_replace in your template file:
Product Description:
{{product_sku}} is the best product ever made!
catalog/product/view.phtml:
getSku(), $_product->getDescription()); ?>
Thank you very much. very usefull
First of all, thank you for this and all the other articles on thie site, you already helped me a lot. I was wondering if it can be achieved to use variables in the product description. At the moment I’ve set a hardcoded link to one of my CMS-pages but I’d like to insert something dynamicly. I’m not willing to update each product description when switching from my development to my productionenvironment (like a widget). I’d be glad to get some help on this, I bet there are several people requesting something like this.
Awesome. Thank you Tsvetan — very helpful.
Hi @Kevin,
the directives are used for static blocks and cms pages …
take a look at:
– for blocks – Mage_Cms_Block_Block _toHtml()
and the line where we call
$html = $processor->filter($block->getContent());
– for cms pages – Mage_Cms_Block_Page _toHtml()
and the line where we call
$html = $processor->filter($this->getPage()->getContent());
you can see the implementation of the code, that parses the directives and “translate them to Magento language” in Mage_Core_Model_Email_Template_Filter also take a look at Varien_Filter_Template
I am not sure what is you idea, but you may use the event ‘core_block_abstract_to_html_before’ … also some other events may help you to manipulate the output.
Good luck and have fun.
Where/how do these directives get called?
I’m trying to intercept them before they get spit out as html.
Great article, I’ll bookmark this one for sure!
I want to include a Static Block in a CMS page and pass a parameter (variable) to the Static Block. How do I do it?
I’m using:
In the CMS Page:
…
{{block id=’static_block_with_var’ var1=’abc’}}
…
This includes the Static Block and presumingly should set var1 to ‘abc’.
In the Static Block I use:
…
{{var var1}}
…
I’m expecting the CMS to display the Static Block and {{var var1}} to be replaced with ‘abc’, but although the Static Pageg is displayed the {{var var1}} is not replaced by ‘abc’.
What am I doing wrong?
@Kevin, Hi.
I am sure, that you can try with {{block …….}}
Take a look at the beginning of the article and especially – 5. blockDirective.
You can pass params to your block, inside your block file to load Magento models …. and even more.
I don’t know nothing about your knowledge, but if you don’t know how, just google or look at this blog how to create your own blocks.
Good luck 🙂
Do you know if there’s an easy way to add your own custom cms syntax? Basically, for the purpose of pulling dynamic php methods into the CMS – for example:
{{helper method=”dynamicPhpMethod” params=”param1″}}
I agree with ANDY,
it’s preatty hANDY!
Handy! Thanks very much