Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Yesterday 23:52:00

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,145
Website GitHub

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

#2 Yesterday 23:57:34

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,090
Website GitHub

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

#3 Today 00:20:09

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,145
Website GitHub

Re: Test if a shortcode outputs anything?

jakob wrote #341558:

Would using test for 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

#4 Today 02:11:52

phiw13
Plugin Author
From: South-Western Japan
Registered: 2004-02-27
Posts: 3,561
Website

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

#5 Today 06:57:45

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,672
GitHub Twitter

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

Board footer

Powered by FluxBB