Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Front-end switch/case tag support
I wrote a plugin to do switch/case years ago and it was horrible and kludgy. Surely we must be able to write a more elegant core tag that would reduce this type of nested control structure:
<div class="article-text"
<if::article_section name="products, services"> itemscope itemtype="https://schema.org/Product"
<txp:else />
<if::article_section name="faq"> itemscope itemprop="mainEntity" itemtype="https://schema.org/Question"
<txp:else />
<if::article_section name="blog"> itemscope itemtype="https://schema.org/BlogPosting"
<txp:else />
...
</if::article_section>
</if::article_section>
</if::article_section>
>
Or the slightly more(?) compute-intensive:
<div class="article-text"
<if::article_section name="products, services"> itemscope itemtype="https://schema.org/Product"</if::article_section>
<if::article_section name="faq"> itemscope itemprop="mainEntity" itemtype="https://schema.org/Question"</if::article_section>
<if::article_section name="blog"> itemscope itemtype="https://schema.org/BlogPosting"</if::article_section>
...
>
With something like this:
<if::switch test='<txp:section />' default='itemscope itemtype="https://schema.org/Article"' wraptag="div" class="article-text">
<txp:matches name="products, services">itemscope itemtype="https://schema.org/Product"</txp:matches>
<txp:matches name="faq">itemscope itemprop="mainEntity" itemtype="https://schema.org/Question"</txp:matches>
<txp:matches name="blog">itemscope itemtype="https://schema.org/BlogPosting"</txp:matches>
...
</if::switch>
Or something like that? Something more efficient and less verbose than using a nested control structure, or having to test a bunch of (assumed, mutually exclusive) values, or having to assign the test to a variable and checking each one using nested <if::variable>
tags.
Syntax and naming tbd, and this will be a Txp 5 thing so no immediate call to action required (unless it’s an absolute cinch to implement now). The difficult part might be a way of making it so that it falls through to the next one or jumps out after N matches have been found (default 1). That could perhaps be handled fairly neatly with an attribute on the surrounding if::switch tag. Say:
match="exact"
ormatch="1"
(one match, then skips the rest)match="any"
(tests each match tag in turn and processes them if they match)match="2"
(tests each match tag in turn and will skip the remaining match tags after 2 successful matches or the end of the set of tags is reached)- …
That sort of aligns it with our existing use of the match
attribute and makes logical sense, and allows for more interesting use cases than simply testing one or all.
The 2nd example above isn’t so bad in terms of how it looks, and it’s probably reaonably performant because the section names are cached after first use, but relies on us having a built-in conditional tag for the thing we want to test. Otherwise we need the txp:variable/if_variable/evaluate trick.
Essentially this proposal is for a modified <txp:evaluate>
tag that can match multiple test cases.
Is this kind of thing doable?
Last edited by Bloke (2025-03-26 15:06:55)
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: Front-end switch/case tag support
I was just looking at some earlier code of mine with smd_switch today and think that would be a great idea. In fact I could use it right away if it were already available :-) So … a 👍 from me.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Front-end switch/case tag support
Well (with his understatement voice), that would rather convenient!
I spend an hour editing (years old convoluted code) my way through a series of nested @<txp:if_section /> block and quite a few times dear TXP was angry with me for not properly closing the tag(s).
So, +2.
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: Front-end switch/case tag support
Cool 👍
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline