Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[solved] txp:evaluate testing multiple custom fields
Am I using this wrong?
<txp:evaluate test="custom_field">
<aside class="complementary-content">
<txp:if_individual_article>
<section>
<h4><txp:text item="links" /></h4>
<txp:if_custom_field name="Music">
<a href="<txp:custom_field name="Music" />" class="link"><img src="/images/BandcampLogo.png" alt="Bandcamp Logo" /></a>
</txp:if_custom_field>
<txp:if_custom_field name="Videos">
<a href="<txp:custom_field name="Videos" />" class="link"><img src="/images/YouTubeLogo.png" alt="YouTube Logo" /></a>
</txp:if_custom_field>
<txp:if_custom_field name="Photos">
<a href="<txp:custom_field name="Photos" />" class="link"><img src="/images/InstagramLogo.png" alt="Instagram Logo" /></a>
</txp:if_custom_field>
<txp:if_custom_field name="Social">
<a href="<txp:custom_field name="Social" />" class="link"><img src="/images/FacebookLogo.png" alt="Facebook Logo" /></a>
</txp:if_custom_field>
</section>
</txp:if_individual_article>
</aside>
</txp:evaluate>
I just get nothing out, even if one or more of the fields contain content. I’d like the entire block to show but only if at least one of the four custom fields contains content. Otherwise I don’t want anything to show as I don’t want the complementary block at all.
If I omit the test
attribute then I get the output regardless, presumably because the <txp:text>
is outputting something. I’ve tried using test="if_custom_field"
and test="custom_field, if_custom_field"
but get the same blank output.
Any ideas where I’m going wrong?
Last edited by Bloke (2019-03-11 09:55:38)
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: [solved] txp:evaluate testing multiple custom fields
Unfortunately, <txp:evaluate />
tests only top-level tags atm, so your only candidate here is if_individual_article
. I have not found an easy way to target more deeply, but it should be possible one day. Meanwhile, you can try
<txp:if_individual_article>
<txp:evaluate test="if_custom_field" wraptag="aside" class="complementary-content">
<section>
<h4><txp:text item="links" /></h4>
<txp:if_custom_field name="Music">
<a href="<txp:custom_field name="Music" />" class="link"><img src="/images/BandcampLogo.png" alt="Bandcamp Logo" /></a>
</txp:if_custom_field>
<txp:if_custom_field name="Videos">
<a href="<txp:custom_field name="Videos" />" class="link"><img src="/images/YouTubeLogo.png" alt="YouTube Logo" /></a>
</txp:if_custom_field>
<txp:if_custom_field name="Photos">
<a href="<txp:custom_field name="Photos" />" class="link"><img src="/images/InstagramLogo.png" alt="Instagram Logo" /></a>
</txp:if_custom_field>
<txp:if_custom_field name="Social">
<a href="<txp:custom_field name="Social" />" class="link"><img src="/images/FacebookLogo.png" alt="Facebook Logo" /></a>
</txp:if_custom_field>
</section>
</txp:evaluate>
</txp:if_individual_article>
Offline
Re: [solved] txp:evaluate testing multiple custom fields
Ahhh, top-level tags only. Missed that vital clue in the docs. Penny drops. Your revised version works brilliantly, thank you.
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: [solved] txp:evaluate testing multiple custom fields
Cool! The problem with “deep” tests is that currently in, say
<txp:evaluate test="inner_tag">
<txp:outer_tag>
<txp:nested_tag>
<txp:inner_tag />
</txp:nested_tag>
Blah
<txp:inner_tag />
Blah blah
</txp:outer_tag>
</txp:evaluate>
the parser must start processing outer_tag
and also nested_tag
before it reaches inner_tag
. It means that actually all tags must be processed, which conflicts with the idea of evaluate
. And we can not start with inner_tag
processing without knowing the context (outer_tag
or nested_tag
) because the result can vary. And what if we need to target inner_tag
only inside nested_tag
?
We could introduce some level
attribute for tested tags nesting depth, but it looks unpractical. I’d rather suggest a global evaluate
attribute for extra flexibility:
<txp:outer_tag evaluate="nested_tag">
<txp:nested_tag evaluate="inner_tag">
<txp:inner_tag />
</txp:nested_tag>
Blah
<txp:inner_tag />
Blah blah
</txp:outer_tag>
Dunno.
Offline
Re: [solved] txp:evaluate testing multiple custom fields
etc wrote #316988:
We could introduce some
level
attribute for tested tags nesting depth, but it looks unpractical.
I almost added a note to that effect, but thought it through for a few minutes and came to the same conclusion. If you insert some new tags in your structure you’d have to re-visit and re-count your levels to make the tag processing continue to work. Plus, it’s not cohesive with the query
attribute.
I’d rather suggest a global
evaluate
attribute for extra flexibility.
That would be my preferred option if it was doable.
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: [solved] txp:evaluate testing multiple custom fields
Bloke wrote #316989:
If you insert some new tags in your structure you’d have to re-visit and re-count your levels to make the tag processing continue to work. Plus, it’s not cohesive with the
query
attribute.
Yes, exactly, glad we think alike :-)
That would be my preferred option if it was doable.
Generally, a container tag calls parse($thing)
, so altering the processing order à la <txp:evaluate />
inside parse()
should be possible.
Offline
Offline