Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#21 2010-10-01 04:32:50

radneck
Plugin Author
Registered: 2005-07-03
Posts: 109

Re: rah_repeat // Split and repeat.

Gocom wrote:

Enjoy pancakes:
Oh, sir, syrup?
Pardon me, your menu is odd. Where’s my donkey?
Wohoo, pony donkey ride!

Offline

#22 2010-10-01 16:59:11

Gocom
Plugin Author
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: rah_repeat // Split and repeat.

cain-mi, you are reading about awesomish plugin that ships with nonexistent free pony donkey and butler, doh.

Offline

#23 2010-11-07 11:00:03

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,036
Website

Re: rah_repeat // Split and repeat.

This plugin gets better every time I use it! Here’s an odd question: how might I split a string according to more than one break ‘trigger’?

The use case is to find out if there are more than one items in a text string and change the header accordingly from singular to plural. I have it working as I want it with break=",", e.g. “cats, dogs and snakes” results in a plural heading, but it doesn’t catch the case “cats and dogs” because has no comma in it. What’d like to do is something like break=",|and" but I’ve no idea whether that works. Any other suggestions?

<txp:variable name="check_multi_items" 
              value='<txp:rah_repeat value=''<txp:custom_field name="species" />'' break="," offset="1" limit="1"><txp:rah_repeat_value /></txp:rah_repeat>' />
   <txp:if_variable name="check_multi_items" value="">
      Singular heading:
   <txp:else />
      Plural heading:
   </txp:if_variable>

TXP Builders – finely-crafted code, design and txp

Offline

#24 2010-11-07 11:24:49

Gocom
Plugin Author
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: rah_repeat // Split and repeat.

jakob wrote:

What’d like to do is something like break=”,|and” but I’ve no idea whether that works. Any other suggestions?

Do you mean, you want it to split the string from both and and ,, or should it form the results with “and”. The break attribute is used to form the resulting list, while delimiter is used to split.

If you want to do split with multiple values, you might be able to nest two tags:

<txp:rah_repeat delimiter="," value='<txp:rah_repeat delimiter="and" value="dog, cat and lemon" break=","><txp:rah_repeat_value /></txp:rah_repeat>'><txp:rah_repeat_value /></txp:rah_repeat>

The first call splits with and while the second one does the commas.

If you want to have and in the resulting list, you could use <txp:rah_repeat_if_last /> conditional. For example:

<txp:rah_repeat value="cat,dog,lemon">
	<txp:rah_repeat_if_first>
		<txp:else />
			<txp:rah_repeat_if_last>
				and
			<txp:else />
				,
			</txp:rah_repeat_if_last>
	</txp:rah_repeat_if_first>
	<txp:rah_repeat_value />
</txp:rah_repeat>

That nested if_first/if_last mess is there because if there is only one value, it’s both, the first and the last. The examples are untested, not sure if the actually work. Be warned ;-)

PS. performance wise the plugin isn’t the best solution for searching needle from haystack as the plugin does much more. It’s awfully slow for just that compared to slimmed down solution. If you just want to know if the custom field has and/comma in there, I would suggest using plain PHP and strpos(). But if you are using rah_repeat for something else and returning the results then go ahead, it’s not bad solution for that at all ;-)

Last edited by Gocom (2010-11-07 11:32:04)

Offline

#25 2010-11-07 15:23:40

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: rah_repeat // Split and repeat.

@Jakob, I think you could easily solve this situation using smd_if, configured for OR logic.

<txp:smd_if field='species' operator="contains|contains" value=",|and" param_delim="|" logic="OR">
Plural
<txp:else />
Singular
</txp:smd_if>

I haven’t tested it, but I think that should work.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#26 2010-11-07 17:53:06

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,036
Website

Re: rah_repeat // Split and repeat.

Thank you both of you. strikes forehead with hand of course, you are both right, it would be much easier to test for the existence of a “comma” or “and”. In that particular case, smd_if works well. Weirdly, Julián’s example didn’t work (txp 4.3 with smd_if 0.9) although it looks absolutely right to me. I checked the docs and slimmed it down further and further until I was testing for a simple known phrase still with no luck. In the end I had to use '<txp:custom_field name="species" />' instead of just "species". Is that possibly a bug in smd_if (the only other difference to the sample here is that my custom field begins with a capital letter)?

<txp:smd_if field='<txp:custom_field name="species" />' operator="contains|contains" value=",|and" param_delim="|" logic="OR">
Plural
<txp:else />
Singular
</txp:smd_if>

TXP Builders – finely-crafted code, design and txp

Offline

#27 2011-12-02 11:23:20

Gocom
Plugin Author
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: rah_repeat // Split and repeat.

Rah_repeat v0.7 released. Changes:

  • Added attribute trim. When set to 1, provided values are trimmed from surrounding whitespace.
  • Fixed: “locale” sorting option. Previously it sorted values as a string, not by locale options.
  • Changed: limit’s default to NULL. Leave limit unset if you only want offset without limit, or use a high value.
  • Improved: Better offset and limit functionality. Now slices the list of values before staring to build the markup.

More info and downloads

Offline

#28 2012-02-02 12:10:28

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: rah_repeat // Split and repeat.

Hi Gocom,
what do you think of adding a “range” attribute?
Not sure if there is something similar in PHP, but I’m thinking about something like range in Python.

Code examples:

<txp:rah_repeat value="00,66,aa,ff" range="10">
  <txp:rah_repeat_iteration />:  <txp:rah_repeat_value />
</txp:rah_repeat>

That will loop over the values 10 times.

<txp:rah_repeat value="00,66,aa,ff" range="a-z">
  <txp:rah_repeat_iteration />:  <txp:rah_repeat_value />
</txp:rah_repeat>

That will loop over the values, 26 times (from a to z).

Will expand (if needed) later, if you think the idea has some merit :)
PS: may x-post this request to smd_each too :)


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#29 2012-02-02 14:31:46

Gocom
Plugin Author
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: rah_repeat // Split and repeat.

maniqui wrote:

what do you think of adding a “range” attribute?

Check out the rah_repeat’s repository and the latest commit ;) (or couple commits ago).

range="min, max [, step]"

Where min and max are required and step is optional. Because combining range and value is very limiting (and also adds complexity to the code), I’ve made range and value VIPs, and the club only has one VIP room. Either it’s one or the other, which gives both the same power; excluding, sorting etc. Nesting can be used to combine their powers. E.g.

<txp:rah_repeat range="0,10">
	<txp:rah_repeat_value />
	<txp:rah_repeat value="00,66,aa,ff"><txp:rah_repeat_value /></txp:rah_repeat>
</txp:rah_repeat>

Last edited by Gocom (2012-02-02 14:48:42)

Offline

#30 2012-02-02 20:45:31

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,036
Website

Re: rah_repeat // Split and repeat.

I’d love to see some real-world usage examples for this and the code it produces as I don’t really understand the example above.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB