a smarty aproach to modX

A way of making modx evolution to use smarty.

For those using modX Evolution who want to have smarty parsing now and then some chunks, this might be a good approach.
First of all I have created a new plugin, called Smarty, linked to the OnParseDocument event.
Its scope is to create a global instance of the smarty parser and to register a new resource parsing method with it, to allow us to parse chunks instead of files from the file system.
In the end we will be able to parse a chunk like this:
<code>
$smarty->fetch('chunk:chunk_name');
</code>
Also if the chunk code will use the inclusion tags, it will work:
<code>
{include file="chunk:another_chunk_name"}
</code>

The plugin source code might be:
<code>
// Get a reference to the event
$e = & $modx->Event;
switch ($e->name) {
 case 'OnParseDocument':
   global $smarty;
     if(!isset($smarty)){
       $basepath = $modx->getConfig('base_path');
       require_once(PATH_TO_SMARTY_LIBS .'/Smarty.class.php');
       $smarty = new Smarty();

        //set delimiters only if you don't like default once
        $smarty->left_delimiter = '<%';
        $smarty->right_delimiter = '%>';
        
        //set compile dir which has to exists and has to be writable
        $smarty->compile_dir = $basepath .'templates_c/';
        
        //define the functions required to register a new resource
        function chunk_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
        {
            global $modx;    
            //read the template source from the chunk
            $tpl_source = $modx->getChunk($tpl_name);
            return true;
        }

        function chunk_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
        {
            //just assume the current time
            //but you can keep record of times and serve them from a database eventualy
            $tpl_timestamp = time();
            return true;
        }

        function chunk_get_secure($tpl_name, &$smarty_obj)
        {
            //assume all templates are secure
            //because they are created in the modX manager zone
            return true;
        }

        function chunk_get_trusted($tpl_name, &$smarty_obj)
        {
            // not used for templates
        }

        // register the resource name "chunk"
        $smarty->register_resource("chunk", array("chunk_get_template",
                                               "chunk_get_timestamp",
                                               "chunk_get_secure",
                                               "chunk_get_trusted"));

}
    break;
}
</code>

As a test you can create a snippet with the folowing code:
<code>
global $modx,$smarty;

$arr = array{'red','blue','green'};
$smarty->assign('colors',$arr);
return $smarty->fetch('chunk:rgb_chunk');
</code>

and the rgb_chunk code:
<code>
<%foreach $colors item=item%>
<p>color <%$item%></p>
<%/foreach%>
</code>

Calling the snippet inside one of your templates should produce:
color red
color blue
color green

Rixe
Posts: 2
Comment
Revolution
Reply #2 on : Wed August 18, 2010, 19:05:40
Hi, just wondering:
have you gotten this nice plugin to work in ModX Revolution yet? I've tried it out and didn't quite get it, but that may be caused by my lack of real php skills ...

Thanks!
Dingo
Posts: 2
Comment
Great!
Reply #1 on : Thu April 29, 2010, 17:04:12
It's just the thing I was looking for last week :) I love MODx Evolution and love Smarty, so, I'm very glad that there is a way to make them friends :)

Write a comment

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.