Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[resolved] Test if custom field content is a list of values
Hi,
I would like to test a custom field to know if it contains several values but the following tag and attributes do not work, probably because “,” is not a valid value as it used to be a separator…
Is there something I can do to make it work? (I know smd_if
but I’m looking for native solution, if there is one).
<txp:if_custom_field name="Image(s)" match="any" value=",">
Do something…
</txp:if_custom_field>
Thanks
Last edited by NicolasGraph (2015-04-02 08:28:38)
Offline
Re: [resolved] Test if custom field content is a list of values
I am wandering if the brackets in the custom field’s name is affecting the result also it could be amended to
<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>
<txp:if_variable name="cfimages" value=",">
Do something…
</txp:if_variable>
The above will/should work only if there is more thank one image as there should be no comma if there is just one. As such the code could be amended to
<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>
<txp:if_variable name="cfimages" value="">
<txp:else />
Do something…
</txp:if_variable>
A combination of the two codes could be used to detect if there is 1 or more images
<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>
<txp:if_variable name="cfimages" value="">
<txp:hide>no images</txp:hide>
<txp:else />
<txp:hide>there is at least one image</txp:hide>
<txp:variable name="alotofimages"><txp:variable name="cfimages" /></txp:variable>
<txp:if_variable name="alotofimages" value=",">
You have more than one image…
<txp:else />
You have just one image
</txp:if_variable>
</txp:if_variable>
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: [resolved] Test if custom field content is a list of values
colak wrote #289674:
I am wandering if the brackets in the custom field’s name is affecting the result
I tried with “Images” but it doesn’t change anything.
also it could be amended to
<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>...
Finally, this will/should work
Unfortunatly it does not, I think because <if_variable name="is_list" value=",">
is the same as <if_custom_field name="images" match="exact" value",">
and not match="any"
which I would need.
Offline
Re: [resolved] Test if custom field content is a list of values
NicolasGraph wrote #289673:
because “,” is not a valid value as it used to be a separator
Correct. But you can harness the power of regexes to do what you want instead:
<txp:if_custom_field name="Images(s)" match="pattern" value="[\,]+">
Do something…
<txp:else />
Don't
</txp:if_custom_field>
Essentially that’s “does the field contain one or more commas?”
Edit: it won’t cater for the situation where your field contains only a single comma, so if you want to ensure that your field actually contains a list of items including at least one comma, you’ll need to modify the pattern a tiny bit.
Last edited by Bloke (2015-04-02 08:14:55)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: [resolved] Test if custom field content is a list of values
Hi Nicolas,
Looking at the if_custom_field specs I guess you are right.
I wonder if you separated the ids of the images with a dash and then replace those values using rah_replace would do the job. I know you said no plugins and I hope somebody will come up for a solution but here is an untested code
<txp:variable name="cfimages"><txp:custom_field name="Images" /></txp:variable>
<txp:if_variable name="cfimages" value="">
<txp:hide>no images</txp:hide>
<txp:else />
<txp:hide>there is at least one image</txp:hide>
<txp:variable name="islist" match="any"><txp:variable name="cfimages" /></txp:variable>
<txp:if_variable name="islist" value="-">
<txp:rah_replace from="-" to=",">
<txp:images id='<txp:variable name="islist" />'><txp:image /></txp:images>
</txp:rah_replace>
<txp:else />
You have just one image
</txp:if_variable>
</txp:if_variable>
> Edit: Stef to the rescue
Last edited by colak (2015-04-02 08:23:09)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: [resolved] Test if custom field content is a list of values
Bloke wrote #289676:
Correct. But you can harness the power of regexes to do what you want instead:
<txp:if_custom_field name="Images(s)" match="pattern" value="[\,]+">...
Essentially that’s “does the field contain one or more commas?”
Edit: it won’t cater for the situation where your field contains only a single comma, so if you want to ensure that your field actually contains a list of items including at least one comma, you’ll need to modify the pattern a tiny bit.
Thank you Stef! I just removed +
and it works with only one comma.
Last edited by NicolasGraph (2015-04-02 08:22:39)
Offline