I’ve found a little problem with MODx and transferring data to a snippet and using it in a string comparison.  The issue arises when you call a snippet from within a page (in the [*content*] portion).  There is also an issue when passing a MODx var in a snippet parameter.  In order to overcome this issue, I’ve needed to create two snippets, one called within the template and another that can be called in the text of [*content*] and get the data from the first snippet.

1. Create a snippet called [[setup]] with this code:

$modx-vars = Array();
$modx->vars['alias'] = $alias;

2. Create another snippet called [[nav]] which would have code like this that would give a class to a navigation link for example:

$alias = $modx->vars['alias'];
echo $alias == 'index'? ' class="current"': '';

3. Then we’ll need a template with this code:

[[setup?alias=[*alias*]&done]]
[*content*]

Note: In the call to [[setup]] we end with &done since the triple end brackets ] cause a problem of their own.

4. Finally create a page with this content:

[[nav]]

Now you can use the $modx->vars array to pass any data you need from one snippet to others when you need to use that data for actual string comparison, manipulation or usage that would otherwise only be accessible after MODx performs a php eval.

Reblog this post [with Zemanta]