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.

	value="JavaScript, jQuery, 1.8.0" 
	assign="language, framework, version"

In the above rah_repeat is supplied with values JavaScript, jQuery and 1.8.0. Until now, the only thing you could have done with the values was to iterate over them. You could of course select a one of the values using the limit and the offset attributes, but that get’s messy and hard to read if you use the same practice frequently.

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.

Further simplification

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.

	Create a variable named highlight that sets what to highlight

<txp:variable name="highlight" value="value2" />

	Iterate and highlight the value that matches the variable's value

<txp:rah_repeat value="value1, value2, value3">
	<txp:if_variable name="highlight" value='<txp:rah_repeat_value />'>
		Highlighting: value2

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 trim="1".

Get it now

You can download rah_repeat v0.8 here for free. Includes a supplementary pony. Believe me, it does. It’s just very tiny and magical. You can follow future development and report issue on GitHub.

Full changelog

  • Fixed: made the sort attribute’s direction optional.
  • Added: exclude can now take and exclude empty strings ("") and zeros (0).
  • Added: range attribute. Allows generating automated lists (range="min, max, step").
  • Added: assign attribute. Allows extracting values as variables.
  • Added: escape attribute to <txp:rah_repeat_value />.
  • Added: Support for natural ordering (sort="natural").
  • Changed: Now trim is enabled by default. Previously values weren’t trimmed from white-space by default.
  • Changed: Renamed locale sorting option to LOCALE_STRING.
  • Changed: Order can be reversed with out re-sorting by using sort="desc".
  • Now requires PHP 5.2 (or newer).

Have tips, tricks or feedback? Let @gocom on Twitter to know.