Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-12-30 01:05:03

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

smd_if: Generic multiple if condition tests

if_this / if_that plugins and tags are great, but sometimes you just need that tiny bit more. Like what if this AND that are true? Nesting of tags is possible, but can become unwieldy with many <txp:else /> clauses.

May I present smd_if as an alternative approach.

Give it a list of virtually any Txp variables, some conditional operators like equals, not, greater than, begins with, contains, defined, etc, and some values to check against and it’ll compare them all. Only if they all match will it do what you ask. It can also use ‘or’ logic to specify that the condition is met if any one of the values match.

Compatible with <txp:else /> and also supports <txp:variable />, URL variables (e.g. only display something if pg > 1?) and server variables (e.g. only display something if the user’s IP address matches something?). The docs explain more

It’s ugly, it’s downright dirty… but it can sometimes get you out of a hole. Use it, abuse it and report any enhancements, ideas, bugs, etc here. Happy New Year to everyone :-)

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.

  • 30 Dec 2007 | v0.10 | Initial release
  • 30 Dec 2007 | v0.20 | Added parent category checking (thanks the_ghost)
  • 02 Jan 2008 | v0.3 | Added defined/undefined and strict numeric comparisons
  • 06 Jan 2008 | v0.40 | Added ? notation to allow the value to read Txp fields. Better quote support (both thanks NeilA)
  • 06 Jan 2008 | v0.41 | Fixed lower case field names and undefined index error (thanks peterj)
  • 14 Jan 2008 | v0.50 | Added case_sensitive option ; made ‘contains’ the default for ‘parent’ tests ; improved help (all thanks the_ghost); added delim options
  • 15 Jan 2008 | v0.51 | Bugfix: defined/undefined no longer error and isused/isempty are more distinct
  • 25 May 2008 | v0.60 | Fixed undefined index errors and chh_if_data problem (thanks redbot/the_ghost). Added more is operators, the ends operator, more pretext variables and file / link support
  • 26 May 2008 | v0.61 | Fix stupid oversight in field name generation to allow arbitrary names instead of forcing $thisarticle (thanks to Joana Carvalho for leading me to this)
  • 11 Jun 2008 | v0.62 | Fixed empty eval returns true and added NULL field type
  • 10 Sep 2008 | v0.70 | Fixed warning if empty custom field in value (thanks visualpeople) ; added txpvar support (thanks the_ghost) ; added thisimage support (for the future) ; added operators in, notin and the list_delim attribute; enabled replacement tags for matched variables
  • 01 Oct 2008 | v0.71 | Fixed the fix for empty custom fields implemented in 0.7 (thanks mapu / visualpeople)
  • 01 Oct 2008 | v0.72 | Added :NOTAGS (thanks mapu)
  • 13 Oct 2008 | v0.73 | Added :NOSPACE to begins, ends and contains (thanks mapu), added phpvar support, :LEN modifier and length replacement tags (all thanks the_ghost)
  • 13 Oct 2008 | v0.74 | Fixed smd_if_ replacement tag bug. Replacement vals/fields are now indexed to avoid being overwritten
  • 13 Oct 2008 | v0.75 | Added divisible operator (thanks gomedia) ; allowed short-circuit of field attribute (thanks redbot)
  • 20 Mar 2009 | v0.76 | Added postvar field type (thanks kostas45)
  • 22 Mar 2009 | v0.77 | Added :TRIM modifier (thanks gomedia)
  • 05 Apr 2009 | v0.80 | Added filtering capability
  • 26 Sep 2009 | 0.81 | Added parent TTL and KIDS modifiers (thanks photonomad) ; improved parent debug output
  • 02 Mar 2010 | 0.82 | Added between and range operators (thanks speeke)
  • 02 Mar 2010 | 0.90 | Internal code refactorisation ; allowed multiple values to be read from multiple sources (thanks speeke) ; enhanced replacement tags
  • 28 Jan 2012 | 0.91 | Fixed pretext checks for section/category (thanks saccade) ; enabled explicit checks for pretext, file, link, image, and article ; added var_prefix to allow nesting of smd_if tags; added :COUNT modifier (thanks the_ghost) ; added :ESC and :ESCALL modifiers (thanks lazlo); fixed checks for defined / undefined
  • 20 Apr 2016 | 0.92 | Added support for ‘thiscategory’, ‘thissection’, ‘thispage’ and ‘thiscomment’ ; added support for prefs matching.
  • 12 Jan 2020 | 1.0.0 | Registered plugin tag; Fixed undefined index warnings.

Last edited by Bloke (2012-01-28 01:15:30)


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 2007-12-30 06:54:19

the_ghost
Plugin Author
From: Minsk, The Republic of Belarus
Registered: 2007-07-26
Posts: 907
Website

Re: smd_if: Generic multiple if condition tests

Just add checking parent category name and it will be the only conditional plugin in my install :)


Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?

Offline

#3 2007-12-30 09:57:43

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

Re: smd_if: Generic multiple if condition tests

the_ghost wrote:

Just add checking parent category name and it will be the only conditional plugin in my install :)

Hehe, hadn’t thought of that. I’ll see what I can do. Mind you, I’m not sure how efficient the plugin is with the eval() calls it does. It may be better to reserve this plugin only for use when you absolutely have to; conventional if_something tags may well be less drain on TXP (just guessing). This is, however, certainly quite a good replacement for a few ‘if’ plugins, and chs_if_urlvar which has a few compatibility issues (TXP warnings) and doesn’t support <txp:else />.

Anyway, when you say “parent” do you mean just check one level ‘up’ the tree or do you mean “is the current category a member of this given parent category at any level”?

EDIT: oh, and do you mean the parent of the current ‘global’ category (?c=) or category1 / category2?

Last edited by Bloke (2007-12-30 18:34:02)


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 2007-12-30 11:58:11

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

Re: smd_if: Generic multiple if condition tests

OK, I went ahead anyway and did it all. Global category, category 1/2 and/or checking of a particular sub-category at any level.

Try v0.2 and see if it meets your needs.


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 2007-12-30 14:10:58

lee
Member
From: Normandy, France
Registered: 2004-06-17
Posts: 831

Re: smd_if: Generic multiple if condition tests

Mr Bloke, this is mega.

Offline

#6 2007-12-30 23:50:24

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: smd_if: Generic multiple if condition tests

This plugin is monstorously useful. I’m in awe.

Offline

#7 2008-01-02 01:53:16

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

Re: smd_if: Generic multiple if condition tests

Major code overhaul (internally) but functionally you shouldn’t spot anything massively different. Mis-spelled variables are now all assumed to be part of the $thisarticle array and will thus return “empty”. Some didn’t do that in earlier versions.

Added two new operator tests: defined and undefined. They differ semantically from isused and isempty (respectively), but the way TXP assigns variables means that, for the most part, they are interchangeable. One caveat is that defined and undefined don’t make sense with parent, so they are forbidden.

Also added a feature to the gt, lt, le and ge operators. You can now specify :NUM on the end of these operators to force the comparison to be integer. This bypasses the problem that the number “5” is technically “less than” the word “squid” when both are treated as strings.

v0.3 is the official release. Share and Enjoy TM


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 2008-01-02 11:30:45

the_ghost
Plugin Author
From: Minsk, The Republic of Belarus
Registered: 2007-07-26
Posts: 907
Website

Re: smd_if: Generic multiple if condition tests

hm… What about adding checking if we have search results? :)


Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?

Offline

#9 2008-01-02 11:36:06

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

Re: smd_if: Generic multiple if condition tests

the_ghost wrote:

hm… What about adding checking if we have search results? :)

No need


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

#10 2008-01-03 14:27:52

peterj
Member
From: Melbourne, Australia
Registered: 2005-06-02
Posts: 99

Re: smd_if: Generic multiple if condition tests

Oh my god, you’ve saved my sanity. Thank you for this most beautiful and flexible plugin,
Peter

Offline

#11 2008-01-04 23:39:19

NeilA
Member
From: Blue Mountains, Australia
Registered: 2004-08-15
Posts: 316
Website

Re: smd_if: Generic multiple if condition tests

Stef,

I perhaps spoke a little too soon over in the other thread

The site under development is here
I have this code on a page, using your plugin:

<code>
<h2>Factory Night Church – Audio Media</h2>
<br />

<ul> <li><a href=”<txp:site_url />fnc/?sort=title”>Title</a></li>
<li><a href=”<txp:site_url />fnc/?sort=series”>Series</a></li> <li><a href=”<txp:site_url />fnc/?sort=date”>Date</a></li> <li><a href=”<txp:site_url />fnc/?sort=author”>Author</a></li>
</ul>
<txp:smd_if field=“urlvar:sort” operator=“undefined” value=”,”>
<h3>FNC Audio Media – by Title</h3><br />
<txp:article listform=“list_title” limit=“10” form=“default” />
<txp:else />
</txp:smd_if>

<txp:smd_if field=“urlvar:sort” operator=“eq” value=“title,” >
<h3>FNC Audio Media – by Title</h3><br />
<txp:article listform=“list_title” limit=“10” form=“default” />
<txp:else />
</txp:smd_if>

<txp:smd_if field=“urlvar:sort” operator=“eq” value=“date,” >
<h3>FNC Audio Media – by Date</h3><br />
<txp:article listform=“list_date” limit=“10” form=“default” />
<txp:else />
</txp:smd_if>

<txp:smd_if field=“urlvar:sort” operator=“eq” value=“author,” >
<h3>FNC Audio Media – by Speaker</h3><br />
<txp:article listform=“list_author” limit=“10” form=“default” />
<txp:else />
</txp:smd_if>

<txp:smd_if field=“urlvar:sort” operator=“eq” value=“series,” >
<h3>FNC Audio Media – by Series</h3><br />
<txp:article listform=“list_series” limit=“10” form=“default” />
<txp:else />
</txp:smd_if>
</code>

This works beautifully for producing the various lists I wanted.
But when I click on the permlink for a single article in the list, I get this error:

Parse error: syntax error, unexpected T_STRING in /home/.akasha/swac/media.swac.org.au/textpattern/lib/txplib_misc.php(534) : eval()’d code(138) : eval()’d code on line 1
Parse error: syntax error, unexpected T_STRING in /home/.akasha/swac/media.swac.org.au/textpattern/lib/txplib_misc.php(534) : eval()’d code(138) : eval()’d code on line 1

And the page only partially displays (although the source indicates that the article is being displayed/generated by TxP)

Any thoughts on what is going wrong?

Thanks!


Neil – Blue Mountains, Australia

http://westserve.org
http://ministrygrounds.net.au

Offline

#12 2008-01-05 16:43:19

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,402
Website GitHub

Re: smd_if: Generic multiple if condition tests

NeilA wrote:

Parse error: syntax error, unexpected T_STRING in /home/.akasha/swac/media.swac.org.au/textpattern/lib/txplib_misc.php(534) : eval()’d code(138) : eval()’d code on line 1

Hi Neil, strange one. Not seen that before with the plugin. I’m trying to replicate it here so I can see what’s causing the error message.

When you say “I have this code on a page” where is the code? Is it in an article in the fnc section, or is it in a form? If so, which form? The default one, or the one assigned to the fnc section? Or on the fnc “page”?

I’ve tried putting the code in the default form and I get a ‘circular reference’ error so I guess it’s not there! I’ll continue poking around and see if I can find anything more, but if you can give me a little more info about how you’ve set it up I can probably work out why it’s falling apart on you.

btw, you may not need the extra commas in the value fields (one value is enough) and you can safely delete the <txp:else /> clauses unless you intend on using them later.

EDIT: Still can’t replicate it here after half an hour or so of trying to break it. Perhaps you might permit me to have a login to your test site? Send me an e-mail if so and I’ll have a poke around.

Last edited by Bloke (2008-01-05 18:26:28)


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