Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2011-12-14 17:32:24
- ploinkr
- Member
- From: Montreal
- Registered: 2007-12-06
- Posts: 83
Checking for presence of image caption
Is that feasible? Here’s my non-working code:
<txp:variable name="capt" value="<txp:upm_article_image><txp:upm_img_caption /></txp:upm_article_image>" />
<txp:if_variable name="capt" value="_%"><txp:upm_article_image><p class="flex-caption"><txp:upm_img_caption /></p></txp:upm_article_image></txp:if_variable>
Thanks for any help!
Ben.
Offline
Re: Checking for presence of image caption
hi Ben.
You have to use single quotes when using txp:tags inside other txp:tags.
Also, I’m not sure about that value="_%"
condition you are testing. Where did you get that one?
This should work:
<txp:variable name="capt" value='<txp:upm_article_image><txp:upm_img_caption /></txp:upm_article_image>' />
<txp:if_variable name="capt" value="">
<!-- nothing here -->
<txp:else />
<txp:upm_article_image><p class="flex-caption"><txp:upm_img_caption /></p></txp:upm_article_image>
</txp:if_variable>
Finally, you could try smd_wrap to shorten your code (and have a great plugin in your arsenal).
Offline
#3 2011-12-14 17:44:55
- ploinkr
- Member
- From: Montreal
- Registered: 2007-12-06
- Posts: 83
Re: Checking for presence of image caption
Thanks so much Julián – that was fast! I tested your code and it works :-D
As for "_%"
, I remember using that as a placeholder for “any” value (similar to the *
boolean operator.) Not sure where I found that, though…
Will take a look at smd_wrap; thanks again!
B.
Offline
Re: Checking for presence of image caption
ploinkr wrote:
As for
"_%"
, I remember using that as a placeholder for “any” value (similar to the*
boolean operator.) Not sure where I found that, though…
Ah, yes, probably from here, but I don’t think that trick will work on your example, as th _%
is related to MySQL queries, and in this case, you are testing just a variable that exists in memory.
Will take a look at smd_wrap; thanks again!
U r welcome.
Offline
Re: Checking for presence of image caption
<txp:chh_if_data><p class="flex-caption"><txp:upm_img_caption /></p></txp:chh_if_data>
Wouldn’t that do it?
Offline
Re: Checking for presence of image caption
If you mean “only show the image if there’s a caption assigned to it” then I agree that maniqui’s suggestion has merit.
But if you want to just display the article image and its (optional) caption then you can use some built-in tags like this and save yourself a plugin:
<txp:images>
<txp:image />
<txp:image_info class="flex-caption" wraptag="p" />
</txp:images>
Unfortunately, if there’s no article image you get all images because I didn’t get my way when the tag was written :-p My argument has always been that you get what you ask for, but every other tag defaults to giving you everything unless you whittle it down, so that’s what happens here for the sake of consistency. If you want to guard against that you can just wrap the entire lot in <txp:if_article_image>
.
For the sake of completeness — and twelve characters — if you want to do maniqui’s code without plugins it looks like this:
<txp:if_article_image>
<txp:variable name="capt" value='<txp:images><txp:image_info /></txp:images>' />
<txp:if_variable name="capt" value="">
<!-- nothing here -->
<txp:else />
<txp:images>
<txp:image />
<txp:image_info wraptag="p" class="flex-caption" />
</txp:images>
</txp:if_variable>
</txp:if_article_image>
I don’t think smd_wrap or chh_if_data offer any tangible benefit in this second case, but I’ve not thought it through.
Last edited by Bloke (2011-12-14 21:53:58)
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