Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-04-10 17:32:55

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 7,755
Website

smd_multi_choice: select... case conditional-based output

To continue the theme of “programmer” tools in TXP, may I present smd_multi_choice.

This one is for when you have to compare one variable against a load of values. Instead of nesting if after if after if, you can check them all in one go in a simpler structure. Plus it has the ability of a ‘default’ branch should any of the preceding tests fail to match, which is very handy when dealing with custom input from the result of HTML forms.

You can do all the usual tests like equals, less than, greater than, is alphanumeric, begins, ends, contains, etc, and compare any TXP variable (using the usual ?my_variable_name system) to a series of values or other variables. In terms of functionality it’s kind of a cross between smd_if and smd_each but with a simpler syntax. The examples show a few of the possible avenues of exploration; the rest is up to you.

Happy Easter :-)

Revision history
————————

All available versions and changes are listed here. Each entry indexes the relevant post(s) in this thread to learn about the features.

  • 10 Apr 09 | v0.10 | Initial release
  • 01 Aug 10 | v0.20 | Added empty type (thanks gomedia) ; added var_prefix ; fixed default case firing unnecessarily ; fixed problem with multiple switch tags on a page

Last edited by Bloke (2010-08-02 22:33: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

#2 2009-04-10 17:51:49

graeme
Plugin Author
Registered: 2004-06-21
Posts: 337
Website

Re: smd_multi_choice: select... case conditional-based output

Sweet :-)

Offline

#3 2009-04-10 21:09:22

jakob
Moderator
From: Germany
Registered: 2005-01-20
Posts: 2,622
Website

Re: smd_multi_choice: select... case conditional-based output

Nice – switch and case for txp, eh?


TXP Builders – finely-crafted code, design and txp

Offline

#4 2009-04-10 21:19:18

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 7,755
Website

Re: smd_multi_choice: select... case conditional-based output

jakob wrote:

Nice – switch and case for txp, eh?

Essentially yes.

But it’s kind of switch/case+ because it’s not just a simple equality tester like most high level languages. The flexibility of TXP tags lends itself to finer-grained control so I took full advantage, to the extent that you can actually mix and match different types of test in the same switch (e.g. if it starts with this or this then do this; if it is greater than such-and-such then do this; if it contains something else then do this; else do that).

It allows you to do some pretty wacky conditional checks that (in PHP at least) would either be impossible, or require skullduggery and preg-match inside each case statement.

Not bad for a morning’s work even if I do say so myself. I’m sure there’s something I’m missing — and no doubt someone will point it out to put me on the right track — but I’m already using it on my upcoming redesign and it makes some of the complex nested ifs superfluous, and the result is a whole lot more readable.

Last edited by Bloke (2009-04-10 21:21:37)


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

#5 2009-04-10 21:54:08

mrdale
Moderator
From: Walla Walla
Registered: 2004-11-19
Posts: 2,198
Website

Re: smd_multi_choice: select... case conditional-based output

I have a perfect job for this. To apply different classes for a product’s markup based on inventory levels.

Chanks, Moneypenny!

Offline

#6 2009-12-01 20:23:23

speeke
Member
From: Bruny Island, Australia
Registered: 2009-03-29
Posts: 161
Website

Re: smd_multi_choice: select... case conditional-based output

I’ve found that <txp:smd_switch item="?section"> works in a form, but <txp:smd_switch item="?s"> is required for use in a page? Is it just me, or is there a quirky Txp explanation for this?

Last edited by speeke (2009-12-01 20:23:42)


“People who value their privileges above their principles soon lose both.”
Dwight D. Eisenhower

Offline

#7 2009-12-01 22:11:07

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 7,755
Website

Re: smd_multi_choice: select... case conditional-based output

speeke wrote:

I’ve found that <txp:smd_switch item="?section"> works in a form, but <txp:smd_switch item="?s"> is required for use in a page

It may depend where you’re looking at the data from. As you probably know, the TXP global variables come in two main arrays when dealing with articles:

  1. $pretext houses the current section (as governed by the URL) and it’s designated $pretext['s'] (the ?s shortcut in the plugin)
  2. $thisarticle holds info about the current article when in individual article mode and its data is read primarily from the database. $thisarticle['section'] (the ?section shortcut in the plugin) holds the current article’s section which should be identical to the URL (one would hope, unless some weird htaccess rewrites are in place!)

Both pretext and thisarticle are set and should always agree when in an article context, but only one is set on a section landing page1. Thus, as a rule of thumb, ?s is accessible anywhere there is a section in the URL (i.e. anywhere except the front page where it is traditionally ‘empty’) and ?section is only available when viewing an article. Perhaps that’s the differentiator here?

1 Note that the form/container inside an article list generated by <txp:article /> or <txp:article_custom /> on a section landing page actually triggers article context, so both variables will be set in your nominated form or container, but outside any article tags only pretext exists… eeek!

Last edited by Bloke (2009-12-01 22:11:27)


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

#8 2009-12-01 23:58:11

speeke
Member
From: Bruny Island, Australia
Registered: 2009-03-29
Posts: 161
Website

Re: smd_multi_choice: select... case conditional-based output

Bloke wrote:

… eeek!

Indeed!!! Many thanks for your explanation.


“People who value their privileges above their principles soon lose both.”
Dwight D. Eisenhower

Offline

#9 2010-07-28 03:49:35

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,167
Website

Re: smd_multi_choice: select... case conditional-based output

Stef, another fine plugin. I’m using it with great success but have a query about empty/unset items.

Consider:

<txp:smd_switch item='<txp:section />'>

I was hoping that it would fall through with a value="" (on an error page <txp:section /> is empty) but it looks like smd_switch doesn’t kick in at all.

Tried using leave_empty="0" but that didn’t do it.

This works OK: Er, I thought it did – but it don’t seem to now, sorry!

<txp:smd_switch item="?section" leave_empty="1">

using value="section".

Help!

In this instance there is a ?equivalent to the TXP tag used in item but if there wasn’t, how might I test for “empty” or “unset”?

Thanks,

Adi

Last edited by gomedia (2010-07-28 05:54:28)

Offline

#10 2010-07-28 14:24:02

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 7,755
Website

Re: smd_multi_choice: select... case conditional-based output

gomedia wrote:

<txp:smd_switch item='<txp:section />'>

Ooh, good call. I’m struggling with how to indicate your intentions here. I’m testing leave_empty="2" which means “If the item is empty, test it against the special value="SMD_EMPTY"”, but that’s getting very confusing. Even more so if you want to use a default at all other times (leave_empty="0") but your value may also be empty.

I think it’s a case of badly chosen attribute names: thinking about it now I’m sure there’s a better way of doing this so I’ll see what I can come up with. Will also try and fix the ‘default not set unless explicitly used in value attribute’ bug that I’ve found.

Thanks for the report.


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

Board footer

Powered by FluxBB