Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Test if a shortcode outputs anything?
I have a complex shortcode that accepts arguments via <txp:yield>, and does nested category_list, section_list, article_custom, variable, if_variable, etc tags inside it.
I’m embedding that shortcode in an article (so probably need to escape it from Textile but that’s a separate issue). However, if it is empty I don’t want to output any preamble text introducing the (non-existent) output. What’s the best way to do this, bearing in mind I’m handing this concept over to a client so it needs to be robust and as short as possible.
I naively thought that this might work:
<txp:evaluate test="stocklist">
Here's the latest stock from our warehouse in this category:
<txp::stocklist cat="crunjo-widgets" />
</txp:evaluate>
But it just renders nothing. If I remove the evaluate wrapper it shows the contents. But, of course, if the site runs out of crunjo-widgets, the entire chunk needs to disappear from the page.
I expect the variable dance would be a workaround. Assign the output of the stocklist tag to a variable then put an if_variable wrapper around the outside of the content and preamble, but that’s a bit convoluted. And I need to explain that to the client and get them to do it on a bunch of articles.
Another option: make a second, wrapper shortcode. Accept the ‘cat’ attribute to pass on to the stocklist shortcode, capture the output, test it with <txp:if_variable> and output the contained text via <txp:yield> and the contents of the variable if it isn’t empty.
Any better ideas?
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: Test if a shortcode outputs anything?
Presumably using test="stocklist" is assuming stocklist is a function but it’s a form. Would using test for some function that the shortcode contains when it runs successfully work here?
TXP Builders – finely-crafted code, design and txp
Offline
Re: Test if a shortcode outputs anything?
jakob wrote #341558:
Would using
testfor some function that the shortcode contains when it runs successfully work here?
Sadly not. I forgot to mention I tried that (using article_custom, which is the main tag that ouptuts stuff inside the form, assuming all the surrounding conditionals match anything).
I seem to recall Oleg mentioning that the test attribute tests things “at the same level” as the evaluate tag, so maybe it can’t go ‘deeper’ into the structure of the stocklist form to match the article_custom because it’s nested inside a vew variable, if_variable and category_list tags.
And even if there was a syntax to do it, if I change how the stocklist works one day by wrapping it in, say, another check, all the articles that use the evaluate tag as a wrapper would stop working.
The variable/if_variable wrapper shortcode works so I might stick with that for the time being, unless anyone can come up with something cleverer.
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: Test if a shortcode outputs anything?
Would using <txp:if_items_count min="1"> help?
<txp:if_items_count min="1">
output your list
</txp:else>
sadly it seems everything vanished
</txp:if_items_count>
(assuming that site runs on 4.9)
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
phiw13 on Codeberg
Offline
Re: Test if a shortcode outputs anything?
What about this one?
<txp:if_yield name="cat">
<txp:variable name="has_cat" value="true" />
<txp:if_variable name="has_cat" value="true">
Here's the latest stock from our warehouse in this category:
<txp::stocklist cat="crunjo-widgets" />
<txp:else />
Sorry for the inconvenience: our stocks are empty...
</txp:if_variable>
</txp:if_yield>
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Test if a shortcode outputs anything?
Pat64 wrote #341563:
What about this one?
Sadly no. That only tests if someone has passed the cat into the shortcode, not if the shortcode itself returns data.
phiw13 wrote #341561:
Would using
<txp:if_items_count min="1">help?
Not sure, I’ll try it, thanks for the idea.
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: Test if a shortcode outputs anything?
There is no stocklist tag, the tags name is output_form:
<txp:evaluate test="output_form">
Here's the latest stock from our warehouse in this category:
<txp::stocklist cat="crunjo-widgets" />
</txp:evaluate>
or a valueless test, or even
<txp:evaluate>
Here's the latest stock from our warehouse in this category:
<txp::stocklist cat="crunjo-widgets" />
</txp:evaluate>
should do.
Another option is
<txp::stocklist cat="crunjo-widgets"
wraptag="Here's the latest stock from our warehouse in this category:<br /><+>"
/>
but it looks weird.
Offline
Re: Test if a shortcode outputs anything?
Ah, didn’t try without the test duh. I presumed it would require it to work. Good call, thank you. That or using output_form as the test should work and makes things neater.
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
Pages: 1