Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2009-01-22 13:48:16
- redbot
- Plugin Author
- Registered: 2006-02-14
- Posts: 1,410
multiple values for attribute "value" in <txp:if_variable>
With a serious dalay I’m now working on a new site using txp 4.07 and I must say it is really impressive, a great step ahead.
Anyway I have a request, even if I realize it probably will not be taken into consideration for upcoming version 4.08: will it be possible to use multiple values for attribute “value” in <txp:if_variable>? I would find it very useful.
P.S. thanks again to all developers for their great work
Last edited by redbot (2009-01-22 13:49:45)
Offline
Re: multiple values for attribute "value" in <txp:if_variable>
A list of values would have to be separated by a special character, presumably a comma, which would then have to be excluded (or escaped) from the individual values.
E.g.:
<txp:if_variable name="foo" value="apple,pear,orange">
tests for either “apple”, “pear”, or “orange”, while
<txp:if_variable name="foo" value="oh\,my,dear,darling">
tests for either “oh,my”, “dear”, or “darling”
Is this a feasible solution?
Offline
#3 2009-01-22 14:36:19
- redbot
- Plugin Author
- Registered: 2006-02-14
- Posts: 1,410
Re: multiple values for attribute "value" in <txp:if_variable>
Wet you’re right, I hadn’t thought about this.
I like your solution, maybe another one could be to wrap in “’” the values containing a comma, like this:
<txp:if_variable name="foo" value="'oh,my',dear,darling">
anyway I’d be happy with both solutions. Thank you!
Offline
Re: multiple values for attribute "value" in <txp:if_variable>
I’d limit functionality to a simple comma separated list, similar to all other TXP tags that allow comma separated values:
<txp:if_variable name="foo" value="apple,pear,orange">
Offline
#5 2009-01-22 18:43:37
- redbot
- Plugin Author
- Registered: 2006-02-14
- Posts: 1,410
Re: multiple values for attribute "value" in <txp:if_variable>
ruud wrote:
I’d limit functionality to a simple comma separated list, similar to all other TXP tags that allow comma separated values:
Yes ruud, this was my initial request, but then there is the problem outlined by wet (if txp:variable has a value which contains a comma).
Offline
Re: multiple values for attribute "value" in <txp:if_variable>
I suspect in most cases one could use different values instead to avoid that problem. Introducing a new escaping method for this purpose is not worth it. Tag functionality is always limited in some way, so this would be a limitation of this particular tag, IMHO. Keep it simple!
Offline
#7 2009-01-23 01:49:29
- redbot
- Plugin Author
- Registered: 2006-02-14
- Posts: 1,410
Re: multiple values for attribute "value" in <txp:if_variable>
Mmm… yes, from my point of view your solution is ok too.
And actually I have no idea how many people are using the <txp:if_variable> and <txp:variable> tags in ‘creative’ ways, so probably you are right when you say it’s not worth the effort.
Well… I realize I can’t add nothing particularly brilliant to the discussion now so I’ll stop talking.
Thank you again
Offline
#8 2009-01-23 10:41:32
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: multiple values for attribute "value" in <txp:if_variable>
You could always add a second attribute, say values?
Offline
Re: multiple values for attribute "value" in <txp:if_variable>
Did this make it into v4.2?
If not, can you do an Indiana Jones and reach back and grab your hat (tag) before the stone rolls shut ;-) ?
(found wet’s suggestion most consistent with other tags)
Last edited by jakob (2009-08-25 20:12:08)
TXP Builders – finely-crafted code, design and txp
Offline
#10 2010-10-14 22:58:43
- redbot
- Plugin Author
- Registered: 2006-02-14
- Posts: 1,410
Re: multiple values for attribute "value" in <txp:if_variable>
I found this old thread by chance and now I’m wondering if there is a slight possibility this will be included in txp 4.3 (yes I know it’s late).
I can’t have a look at the code but maybe something could be borrowed from the recent changes in “if_custom_field” tag?
Offline
Re: multiple values for attribute "value" in <txp:if_variable>
ruud wrote:
I’d limit functionality to a simple comma separated list, similar to all other TXP tags that allow comma separated values:
<txp:if_variable name="foo" value="apple,pear,orange">
What’s the current way to do this? Is there any native way? Is smd_if the only option? Is there some other method?
Thank you!
[Edit: Fix bq formating.]
Last edited by johnstephens (2011-03-23 16:05:47)
Offline
#12 2011-03-23 21:29:11
- els
- Moderator

- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: multiple values for attribute "value" in <txp:if_variable>
If you don’t need to test against too many values, you can nest the if_variable tags:
<txp:if_variable name="foo" value="apple">
OK
<txp:else />
<txp:if_variable name="foo" value="orange">
OK
<txp:else />
<txp:if_variable name="foo" value="pear">
OK
<txp:else />
Not OK
</txp:if_variable>
</txp:if_variable>
</txp:if_variable>
Offline
Re: multiple values for attribute "value" in <txp:if_variable>
smd_if has ya covered.
Offline
Re: multiple values for attribute "value" in <txp:if_variable>
Cool, thanks citizens! I installed smd_if earlier today and got it working flawlessly.
Offline
#15 2012-01-24 08:25:54
- makss
- Plugin Author
- From: Ukraine
- Registered: 2008-10-21
- Posts: 355
Re: multiple values for attribute "value" in <txp:if_variable>
wet wrote:
<txp:if_variable name="foo" value="oh\,my,dear,darling">
tests for either "oh,my", "dear", or "darling"
Maybe add separator ?
<txp:if_variable name="foo" value="oh,my@@dear@@darling" separator="@@">
patched if_variable
function if_variable($atts, $thing = NULL)
{
global $variable;
extract(lAtts(array(
'name' => '',
'value' => '',
'separator' => '' // add this line
), $atts));
if (empty($name))
{
trigger_error(gTxt('variable_name_empty'));
return;
}
if (isset($variable[$name]))
{
if (!isset($atts['value']))
{
$x = true;
}
else
{
// $x = $variable[$name] == $value; // change this line
$x = ($separator !='') ? in_list($variable[$name], $value, $separator) : $variable[$name] == $value;
}
}
else
{
$x = false;
}
return parse(EvalElse($thing, $x));
}
upd: fixed, thanks Gocom
Last edited by makss (2012-01-24 11:33:32)
aks_cron : Cron inside Textpattern | aks_article : extended article_custom tag
aks_cache : cache for TxP | aks_dragdrop : Drag&Drop categories (article, link, image, file)
Offline