Textpattern 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: 5,841
Website

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

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.

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: 5,841
Website

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.

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: 5,841
Website

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.

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
Moderator
From: Walla Walla
Registered: 2004-11-19
Posts: 2,059
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: 5,841
Website

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.

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: 5,841
Website

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.

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: 25

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

Board footer

Powered by FluxBB