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.
Image: rah_repeat
List of features
- Split list of values into tiny blocks and represent them individually.
- Exclude values, remove dublicates.
and do custom lists.
Requirements
- PHP 4.3.0+
- Textpattern 4.x.x+
Recommended:
- Textpattern 4.0.8+
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 suplied with defined value(s).
Attributes
The main container tag is <txp:rah_repeat> and attributes for it follow.
value
Value to be split and suplied. Default is unset.
Example: value="dog,cat,human" Default: ""
delimiter
Delimiter used to split the defined value into multiple values. Default is comma.
Example: delimiter="|" Default: ","
duplicates
Remove dublicate values from the list. If the attribute is set to 1, only first appearance of the value is used, duplicates are stripped out. Default is 0, duplicates are not removed.
Example: dublicates="1" Default: "0".
exclude
Exclude certain values from the list. Comma (or delimiter) seperated list if multiple. Default is unset (""), nothing is excluded.
Example: exclude="foo,bar" Default: ""
sort
Sort the values. If the attribute is used, all values are rearranged to correct order. Allowed values: regular (sorts without checking type), numeric (sort in numeric order), string (sort as strings) and locale (sort according locale settings), and appending order desc and asc. Default is unset (""), values are returned in the order they are feeded.
Example: regular asc. Default: "".
offset
The number of items to skip. Default is 0, no offset is being used.
Example: offset="5" Default: "0"
limit
The number of items to display. Note that skipped items (offset option) are count into the limit. Default is unset, no limit is being 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:
<txp:rah_repeat_value />
Is a single tag. It has no attributes. It’s used to deliver split values. See examples.
<txp:rah_repeat_if_first>
Is a container tag. It has no attributes. Returns contained statement if the current item is the first item.
<txp:rah_repeat_if_last>
Is a container tag. It has no attributes. Returns contained statement if the current item is the last item.
Examples
- Simple usage
- Tags inside tags
- Using offset and limit
- Repeat inside repeat
- First and last
- Trim whitespace from the value
- Remove duplicate values
- Rearrange the list
- Exclude values
Example #1: Simple usage.
This example turns simple comma seperated 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: Tag inside tags.
From Textpattern version 4.0.7 and above you can use tags within tags or tags as tag attributes values. We can use that as our weapon.
Let’s say that you have comma seperated list of items stored inside article’s custom field. For example, list of “Nameless” videservice’s video IDs (ID1,ID2,ID3,ID4). Thus you need to render video player code for every ID.
We use <txp:custom_field /> tag as our id, 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>
Outputs 4 videoplayers.
Example #3: Using Offset and limit.
First display two items, then some text between, two items more, some more text and then the rest of 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:
<ul>
<li>group1</li>
<li>item1</li>
<li>item2</li>
</ul>
<ul>
<li>group2</li>
<li>item1</li>
<li>item2</li>
</ul>
Example #5: First and last.
<txp:rah_repeat value="foo,bar">
<txp:rah_repeat_if_first>
<ul>
</txp:rah_repeat_if_first>
<li><txp:rah_repeat_value /></li>
<txp:rah_repeat_if_last>
</ul>
</txp:rah_repeat_if_last>
</txp:rah_repeat>
Returns:
<ul>
<li>foo</li>
<li>bar</li>
</ul>
Example #6: Trim begining and ending whitespace from the value by using rah_function.
<txp:rah_repeat value="foo, bar">
<txp:rah_function call="trim">
<txp:rah_repeat_value />
</txp:rah_function>
</txp:rah_repeat>
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.6
- Added attribute:
exclude. - Fixed unworking
<txp:rah_repeat_if_last>tag caused by v0.5 update.
Version 0.5
- Changed offsets default value from
unsetto0. - Added attribute:
sort. - Added attribute:
duplicates.
Version 0.4
- Fixed: now returns old parent global, if two tags are used inside each other, instead of defining it empty.
- Added:
<txp:rah_repeat_if_first>. - Added:
<txp:rah_repeat_if_last>.
Version 0.3
- Added attribute:
wraptag. - Added attribute:
break. - Added attribute:
class.
Version 0.2
- Added attribute:
limit. - Added attribute:
offset.
Version 0.1
- First release