smd_if

For more information on usage, bugs, features etc, please visit the forum thread. For other software by me, or to make a donation, see the software page.

Contents

Overview

A really simple, generic 'if condition' tester. Can test any field in the current article or list context for a variety of attributes and take action if true or false.

Features:

Attributes

Three mandatory comma-separated lists as follows:

The comma separated lists are processed in order, i.e. the 1st field uses the 1st operator and compares it to the 1st value; the 2nd field uses the 2nd operator in the list and compares it to the 2nd value, and so on.

field

List of TextPattern fields to look at. Possible values are:

If you specify a field name that does not exist, the text you use will be taken verbatim.

There is one special field parent that checks the parent category for a match. Specifying 'parent' on its own will use the global category (?c=). If you add :CATn to the option (where 'n' is 1 or 2) it will instead compare category1 or category2 respectively. If you add :LVLn to the option, the comparison will be restricted to that 'level' of sub-category. You can use CAT and LVL in combination, independently or not at all. This allows comparisons such as "if the 2nd sub-category of category1 contains blahblah" or "if category2 is a child of blahblah". See Example 4.

operator

List of operators to apply, in order, to each field. Choose from:

value

List of values to compare each field in turn to. Can be static values/text or the name of any TXP field, exactly like those given in field (except 'parent').

logic

Can be one of and (the default) or or. If using and, all conditions must be met for a TRUE result. If using or, any of the conditions that match will give a TRUE result.

Examples

Example 1

<txp:smd_if field="section, id"
     operator="begins, gt"
     value="lion, 12" >
 <txp:article>
<txp:else />
 <p>Not today, thanks</p>
</txp:smd_if>

Checks if the current section name begins with the word 'lion' and the article ID is greater than 12. Displays the article if both conditions are met or displays the text "Not today, thanks" if not.

Example 2

<txp:smd_if field="summary, category, urlvar:pic"
     operator="isused, eq, isnum"
     value=", animal ," >
 <txp:article>
<txp:else />
 <p>Not today, thanks</p>
</txp:smd_if>

Checks if the custom field labelled 'summary' has some data in it, checks if the global category equals 'animal' and tests if the urlvar pic is numeric (e.g. ?pic=5). If all these conditions are met the article is displayed, else the message is shown. Note that isused and isnum don't take arguments for value and their positions are held by empty commas (technically the last comma isn't needed but it helps keep everything neat if you add further tests later on).

Example 3

<txp:smd_if field="article_image, svrvar:HTTP_USER_AGENT"
     operator="eq, contains"
     value="urlvar:pic, Safari"
     logic="or">
 <txp:article>
<txp:else />
 <p>Not today, thanks</p>
</txp:smd_if>

Compares (for equality) the current article image id with the value of the url variable pic and checks if the value of the HTTP_USER_AGENT string contains 'Safari'. This example uses the 'or' logic, hence if either condition is met the article is shown, otherwise the 'not today' message is displayed.

Example 4

<txp:smd_if field="parent:LVL2"
     operator="eq"
     value="mammal"
     logic="or">
 <txp:article>
<txp:else />
 <p>Not today, thanks</p>
</txp:smd_if>

Checks the 2nd sub-category of the global category's tree to see if it equals "mammal". If it does, show the article; if not show the message. Remove the :LVL2 to see if the current global category is a child of 'mammal' (at any level). Change the field to parent:CAT1 to see if the article's category1 matches 'mammal' at any level or use field="parent:CAT1:LVL2" to combine the checks.

Changelog