web technology is a way of life
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.
The Livescribe Pulse smartpen gets an app store. This little pen is quite amazing, and it just shows that the business model of an app store is already changing the market for every type of electronic device. So when does my…
The Ghostwire (DSiWare) game looks great and reminds me a lot of Fatal Frame.
FloorPlanner is an interesting bit of software built in Flash that allows you design your home in 2D and view in 3D.
The new Stargate Universe [Philosphocal] teaser looks awesome. I stopped watching Atlantis a bit ago because it got boring, I hope this pumps up the energy as BSG did. It has Lou Diamond Philips and Ming-Na in it, that could make…
Here’s a plethora of well designed RSS Feed icons for free!
3 Responses for "MODx: data passing between snippets"
Interesting approach for resolving this problem. That is one of the cool things about MODx it lets you code something simple to achieve your objectives.
The problem you are describing is related to the sequence when items get parsed. For one snippet to know the value of the second, it needs to have that data available and if it hasn’t been parsed yet it is not yet available. Another way to overcome this is to have one snippet called un-cached by using the [! !] syntax and the second snippet called using the standard cached syntax [[ ]]. This causes one snippet to be processed on one pass of the parser making its value available for the next pass which will handle the [[ ]] snippet calls.
You could also do this with a plug-in as well. So many options in MODx…it’s all about solutions :)
Your approach is good, since it uses less passes of the parser it may even be more efficient (I don’t know), thanks for posting this.
Also note that you can avoid the need for the &done by wrapping the placeholder in back-ticks:
[[setup?alias=`[*alias*]`]]
Shane,
I’ve been using MODx for only a few sparse projects here and there, so I wasn’t aware of the [! !] or `[* *]` syntax. I’ll have to try these both out. Thanx.
MODx is by far the best CMS for programmers that I’ve used since it allows simple direct PHP integration without the need for plugins or modifying the codebase.
You will enjoy this article that describes how the MODx parser works: http://www.sottwell.com/how-modx-works.html