rah_repeat

Published under current TXP projects

Split, loop and repeat a value by value. With this Textpattern plugin, you can turn a simple list of values into advanced HTML output. Split markup half, and represent halves in different places on the page. Do more, use less code by using the same code again.

Download rah_repeat v0.7

Image: rah_repeat

Image: rah_repeat

List of features

Requirements

Recommended:

Installation and usage

The general behavior stands: paste plugin code to the plugin installer textarea and run the automatic setup. Then just activate the plugin and you are ready to use new tags that plugin includes like others.

Basically just put wrap (contain) content in your pages/forms/articles with <txp:rah_repeat></txp:rah_repeat> container tags, and customize output with your desired attributes. Contained content will be repeated and supplied with defined value(s).

Attributes

The main container tag is <txp:rah_repeat> and attributes for it follow.

value
Sets the values that are passed to the tag.
Example: value="dog,cat,human" Default: ""

delimiter
Sets the delimiter used to split the provided value into list of multiple values. Default delimiter is comma (,).
Example: delimiter="|" Default: ","

duplicates
Removes duplicate values from the list. If the attribute is set to 1, only first appearance of the value is used, duplicates are stripped off. Default is 0, duplicates are not removed.
Example: duplicates="1" Default: "0".

exclude
Exclude certain values from the list. Comma (or delimiter, if delimiter is changed) separated list of values if multiple. Default is unset (""), and nothing is excluded.
Example: exclude="foo,bar" Default: ""

trim
If set to 1, the provided values are trimmed from surrounding extra whitespace (spaces, line-breaks). This can be helpful if the provided values are from user-input (i.e. from article field), or the values just have extra whitespace, and the resulting output has to be clean (i.e. posed to XML, JavaScript, or to a variable for comparison). By default the option is off, and values are not trimmed.
Example: trim="1" Default: 0

sort
Sort the values. If the attribute is used, all values are rearranged to the specified order. Allowed values are regular (sorts without checking type), numeric (sorts in numeric order), string (sort as strings) and locale (sort according server’s locale settings), followed by sorting order, either desc and asc. Default the option isn’t used (unset), and the values are returned in the order they were supplied.
Example: sort="regular asc". Default: "".

offset
The number of items to skip. Default is 0, no offset is used.
Example: offset="5" Default: "0"

limit
The number of items to display. By default not limit is used.
Example: limit="10" Default: ""

wraptag
The (X)HTML tag, without brackets used to wrap the output. Default is unset.
Default: wraptag="" Example: "div"

break
The (X)HTML tag (without brackets) or string to separate list items. Default is unset.
Default: break="" Example: "br"

class
The (X)HTML class applied to the wraptag. Default is unset.
Default: class="" Example: "plugin"

The plugins has three more extra tags. None of extra the tags have attributes, and should be used wrapped inside <txp:rah_repeat></txp:rah_repeat> tags. The tags are as follows.

<txp:rah_repeat_value />
Is a single tag, and has no attributes. It’s used to display the provided values, and should be used wrapped inside <txp:rah_repeat></txp:rah_repeat> tags. See examples for some usage instructions.

<txp:rah_repeat_if_first>
Is a container tag, and has no attributes. Returns contained statement if the current item is the first item.

<txp:rah_repeat_if_last>
Is a container tag, and has no attributes. Returns contained statement if the current item is the last item.

Examples

Example #1: Simple usage.

This example turns simple comma separated list of dog,cat,human into a HTML list.

<txp:rah_repeat wraptag="ul" break="li" value="dog,cat,human">
	A <txp:rah_repeat_value />.
</txp:rah_repeat>

Returns:

<ul>
	<li>A dog.</li>
	<li>A cat.</li>
	<li>A human.</li>
</ul>

Example #2: Tags as values.

As of Textpattern version 4.0.7, you can use tags inside tags.

Let’s say that you have comma separated list of items stored inside article’s custom field. For example, list of “Nameless” video service’s video IDs (ID1,ID2,ID3,ID4), and you want to embed each of those as a playable video.

We pass the custom field hosting the video IDs to rah_repeat tag (with the value attribute), and place the video player code inside the container:

<txp:rah_repeat value='<txp:custom_field name="MyCustomFieldName" />'>
	<object width="600" height="380">
		<param name="movie" value="http://example.com/v/<txp:rah_repeat_value />"></param>
		<embed src="http://example.com/v/<txp:rah_repeat_value />" width="600" height="380"></embed>
	</object>
</txp:rah_repeat>

Above code would output 4 embedded players (one for each clip), displaying the videos specified with the custom field.

Example #3: Taking advantage of offset and limit.

First display two items, then some text between, two more items, some more text and then the rest of the items.

<txp:rah_repeat value='<txp:custom_field name="MyCustomFieldName" />' limit="2">
	<txp:rah_repeat_value />
</txp:rah_repeat>
<p>Some text here.</p>
<txp:rah_repeat value='<txp:custom_field name="MyCustomFieldName" />' offset="2" limit="4">
	<txp:rah_repeat_value />
</txp:rah_repeat>
<p>Some another cool phrase here.</p>
<txp:rah_repeat value='<txp:custom_field name="MyCustomFieldName" />' offset="4">
	<txp:rah_repeat_value />
</txp:rah_repeat>

Example #4: Repeat inside repeat.

<txp:rah_repeat value="group1|item1|item2,group2|item1|item2">
	<ul>
		<txp:rah_repeat value='<txp:rah_repeat_value />' delimiter="|">
			<li><txp:rah_repeat_value /></li>
		</txp:rah_repeat>
	</ul>
</txp:rah_repeat>

Returns two HTML lists:

<ul>
	<li>group1</li>
	<li>item1</li>
	<li>item2</li>
</ul>
<ul>
	<li>group2</li>
	<li>item1</li>
	<li>item2</li>
</ul>

Example #5: Basic usage of if_first and if_last.

With the conditional tags <txp:rah_repeat_if_first /> and <txp:rah_repeat_if_last> we can test which value is the first and which is the last.

<txp:rah_repeat value="item1,item2,item3,item4,item5" wraptag="ul" break="li">
	<txp:rah_repeat_if_first>First: </txp:rah_repeat_if_first>
	<txp:rah_repeat_if_last>Last: </txp:rah_repeat_if_last>
	<txp:rah_repeat_value />
</txp:rah_repeat>

Returns:

<ul>
	<li>First: item1</li>
	<li>item2</li>
	<li>item3</li>
	<li>item4</li>
	<li>Last: item5</li>
</ul>

Example #6: Trim beginning and ending whitespace from the value

<txp:rah_repeat value="foo, bar" trim="1" break=","><txp:rah_repeat_value /></txp:rah_repeat>

Returns: foo,bar (notice the missing space before bar).

Example #7: Remove duplicate values

<txp:rah_repeat break="," duplicates="1" value="foo,bar,bar,foo,bar,bar,foo,foobar">
	<txp:rah_repeat_value />
</txp:rah_repeat>

Returns: foo, bar, foobar

Example #8: Arrange the values from lowest to highest

<txp:rah_repeat break="," value="b,a,c," sort="regular asc">
	<txp:rah_repeat_value />
</txp:rah_repeat>

Returns: a, b, c

Example #9: Exclude values

<txp:rah_repeat value="foo,bar,foobar" exclude="foo,bar">
	<txp:rah_repeat_value />
</txp:rah_repeat>

Returns: foobar

Changelog

Version 0.7

Version 0.6

Version 0.5

Version 0.4

Version 0.3

Version 0.2

Version 0.1