Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
nest a conditional statement
In a form I want to check if there are any values stored in either, or both, of two custom fields in an article.
There are 3 possible positive outcomes, that i am calling a, ab & b.
I tried to nest this if_custom_field conditional statement:
<code><txp:if_custom_field name=“a”>
<txp:if_custom_field name=“b”>
outcome ab
<txp:else />
outcome a
</txp:if_custom_field>
<txp:else />
<txp:if_custom_field name=“b”>
outcome b
</txp:if_custom_field>
</txp:if_custom_field>
</code>
Then I checked the wiki and read
Conditional tags may be nested, unless you are nesting a tag within the same tag
:o(
Is there an alternative way to do this?
I’m guessing there must be a way to write a conditional in php, but I am only a simple designer who got ideas above his station.
Any suggestions appreciated
Offline
Re: nest a conditional statement
pieman,
You might wince at me and say “not like that stupid”, but here goes. I don’t think you need to nest these to get what you are talking about.
<code><txp:if_custom_field name=“a”>output “a”</txp:if_custom_field>
<txp:if_custom_field name=“b”>output “b”</txp:if_custom_field></code>
This automatically produces outcome ab, if “a” and “b” are present and leaves “a” or “b” blank if they are not present.
Depending on how you display or lay them out will give you the results you need.
See this post for details:
Hope that helps.
Matthew
- I am Squared Eye and I
am launchinghave launched Pattern Tap
Offline
Re: nest a conditional statement
heh
thanks Matthew
That makes sense, but I guess I didn’t put my example in enough context.
I’m using the conditionals to create a class attributes inside an anchor and add values to it.
<code><a href=”<txp:permlink />”
<txp:if_custom_field name=“a”>
<txp:if_custom_field name=“b”>
class=“a b”“
<txp:else />
class=“a”
</txp:if_custom_field>
<txp:else />
<txp:if_custom_field name=“b”>
class=“b”
</txp:if_custom_field>
</txp:if_custom_field>
<txp:title /></a></code>
I only need to generate the <code>class=” “</code> attribute once. (but potentially add two values to it).
With the example you suggested, for the ab outcome I would get <code>class=“a” class=“b”</code>.
Or if I took the <code>class=” “</code> out of the conditional and wrapped it around the outside I would risk getting an empty attribute if neither were true.
Offline
Re: nest a conditional statement
Pieman,
Please show me exactly what you are trying to do. Maybe I can help you with a different solution?
Sorry the last one didn’t do it :)
- I am Squared Eye and I
am launchinghave launched Pattern Tap
Offline
Re: nest a conditional statement
no worries…
I’m using rss_suparchive to generate a list of articles as the local navigation for a section
- the list should be present in article-list and individual-article pages.
The section is for events listings.
After a published event has passed, it will be possible (but not compulsory) to add extra content to it, using 2 custom fields – called ‘mp3’ and ‘flickr’ (a file number and a flickr tag respectively).
Because I may be will adding extra content to the article some time after it was first published, I need to signal the presence, or otherwise, of this added content in the navigation.
I decided to give each link a class name for each of the custom fields that has content.
The purpose being that I can use CSS to apply a background image to each link based on the class name, and demonstrate the added content or otherwise. So there are 3 possible class names.
Hope that makes sense.
- if a past event has an mp3 attached but no photos the link in the navigation would have a class value of “mp3” (outcome a)
<code><a href=”<txp:permlink />” class=“mp3”><txp:title /></a></code> - if a past event has an mp3 and photos attached, the link in the navigation would have a class value of “mp3flickr” (outcome ab)
<code><a href=”<txp:permlink />” class=“mp3flickr”><txp:title /></a></code> - if a past event has no mp3 but has photos attached, the link in the navigation would have a class value of “flickr” (outcome c)
<code><a href=”<txp:permlink />” class=“flickr”><txp:title /></a></code> - and in the cases where neither custom fields had any content, the class attribute should not even be written, else I think an empty attribute may cause a validation error.
<code><a href=”<txp:permlink />”><txp:title /></a></code>
Last edited by pieman (2006-01-08 14:32:34)
Offline
Re: nest a conditional statement
Pieman,
Sorry if I’m wrong here, but couldn’t you do something like this with the class?
class=“link< custom field mp3 >-< custom field flickr >”
So that if it those custom fields were empty you would still have class=“link” and the unparsed if_custom and custom tags would vanish?
Seems like that would work?
I haven’t used suparchive though, you might check in that plugin thread to get a little more help if my idea doesn’t work?
Matthew
- I am Squared Eye and I
am launchinghave launched Pattern Tap
Offline
Re: nest a conditional statement
Yeah, that would work…
thanks for your ideas Matthew :)
Offline
Re: nest a conditional statement
Hey, great!
I’m glad.
- I am Squared Eye and I
am launchinghave launched Pattern Tap
Offline
Pages: 1