5

I'm building a module. I want to be able to use a shortcode (template directive) in my CMS static blocks, such as the following:

{{store url=""}}

I want to call my own module's frontpage blocks using this method. Is this even possible? Google has turned up nothing about writing my own.

1

2 Answers 2

9

You need to override this model: Mage_Cms_Model_Template_Filter Mage_Widget_Model_Template_Filter and add a method for your own shortcode. Let's assume your short code is called 'custom', you need to add a method called customDirective($construction) that handles your shrotcode.
To get an idea on how that method should look like, take a look at Mage_Core_Model_Email_Template_Filter::storeDirective(). This method handles the store shortcode.

To get the parameters passed to the short code use this:

$params = $this->_getIncludeParameters($construction[2]); 

After processing them you need to return a string value that will replace the {{custom ...}} placeholder.

[Edit]
I realized my mistake. The Widget module changes the template processor. Here is the piece of code that worked for me.
file app/etc/modules/Easylife_Widget.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Widget>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Widget />
            </depends>
        </Easylife_Widget>
    </modules>
</config>

file app/code/local/Easylife/Widget/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Widget>
            <version>0.0.1</version>
        </Easylife_Widget>
    </modules>
    <global>
        <models>
            <widget>
                <rewrite>
                    <template_filter>Easylife_Widget_Model_Template_Filter</template_filter>
                </rewrite>
            </widget>
        </models>
    </global>
</config>

file app/code/local/Easylife/Widget/Model/Template/Filter.php

<?php

class Easylife_Cms_Model_Template_Filter extends Mage_Widget_Model_Template_Filter{
    public function customDirective($construction){
        $params = $this->_getIncludeParameters($construction[2]); 
        return print_r($params, 1);
    }
}

Clear the cache.
Now when I enter {{custom}} in the page content I get back Array().
If I enter {{custom var1="6"}} I get Array ( [var1] => 6 ).

4
  • You say override this model Marius, is there a way to extend it rather? I assume I need to reference my new model in the config.xml file?
    – Jongosi
    Commented Jun 20, 2013 at 11:48
  • I've edited the answer to make it more clear.
    – Marius
    Commented Jun 20, 2013 at 11:55
  • I've edited the answer again. I overlooked something the first time.
    – Marius
    Commented Jun 20, 2013 at 15:56
  • Yes, that works perfectly now. Can I "mark as answer" twice? Thanks so much Marius.
    – Jongosi
    Commented Jun 20, 2013 at 16:08
4

Given the popularity of this question, thought I'd add this; there's (obviously) a similar way to mimic this functionality. If the block type of core/template is used, we can access the data array using getData().

For example, if we create a block in CMS similar to the following:

{{block type="core/template" name="a_sweet_title" bimg="my_profile_pic.jpg"
    bwidth="250" bheight="500" template="mymodule/my_template_file.phtml"}}

Then we can access that data in the phtml file using getData():

$imgSrc = $this->getData('bimg');
$blockWidth = $this->getData('bwidth');
$blockHeight = $this->getData('bheight');

Or, better yet, even retrieve a record/collection using a block parameter:

$collection = Mage::getModel('mymodule/mymodule')->load($theId);

Armed with that knowledge, maybe ask yourself; 'Do I really need a custom shortcode'? Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.