Today in the mist of upcoming Textpattern 4.5, we put out the next version of rah_repeat, v0.8. This update concentrates making the tag’s syntax easier read and user friendlier. The main new thing added in is the variable extraction and assigning.
Assigning variables like a boss
It’s easy too, one attribute, some names for variables and a comma or two. I would say the hardest part is making up the names for variables that fight from the same namespace.
The rah_repeat tag has a new attribute named assign, which takes a comma-separated list of variable names. Each these variables gets populated with one of the values specified with the value attribute.
That’s history. Now with the assign you can just merely create variables from the values without being forced to do iterations or use nasty list tag like hacks. The above snippet would create three variables named language, framework and version, each containing one of the values, which then can be used for anything:
<p>This site's <txp:variable name="language" /> is written using <txp:variable name="framework" /> framework. Currently at version <txp:variable name="version" /></p>
Pretty cool, huh? Results in stupidly readable code.
It’s been pretty clear that in most cases you don’t want the white-space in the value list. You want to use it to make the value list readable and pretty, but you don’t want to carry it over to the iteration. The rah_repeat_value really shouldn’t return stray spaces.
In the previous release tagged 0.7, this was solved by introducing a trim attribute. When enabled, extra white-space was trimmed from the supplied values. Thing was, this attribute wasn’t enabled by default. To further ease thing out, now it is. I figured out that’s what most want.
<txp:hide> Create a variable named highlight that sets what to highlight </txp:hide>
<txp:variable name="highlight" value="value2" />
<txp:hide> Iterate and highlight the value that matches the variable's value </txp:hide>
<txp:rah_repeat value="value1, value2, value3"> <txp:if_variable name="highlight" value='<txp:rah_repeat_value />'> Highlighting: value2 </txp:if_variable> </txp:rah_repeat>
Previously the above would have never returned Highlighting: value1 line since values would have included the white-space. Well, not without complicating the code with a extra
Get it now
- Fixed: made the
sortattribute’s direction optional.
excludecan now take and exclude empty strings (
"") and zeros (
rangeattribute. Allows generating automated lists (
range="min, max, step").
assignattribute. Allows extracting values as variables.
- Added: Support for natural ordering (
- Changed: Now
trimis enabled by default. Previously values weren’t trimmed from white-space by default.
- Changed: Renamed
localesorting option to
- Changed: Order can be reversed with out re-sorting by using
- Now requires PHP 5.2 (or newer).
Have tips, tricks or feedback? Let @gocom on Twitter to know.