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.
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:
eval()
command which most programmers concur should be renamed evil()
Three mandatory comma-separated lists as follows:
field
: the TXP fields to testoperator
: the operations for comparison with that field (eg "eq", "not", "begins" etc)value
: the values to compare the fields toThe comma separated lists are processed in order, i.e. the 1st field uses the 1st operator in the list 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:
section
category
(or category1
, or category2
)authorid
id
(article ID)query
(the query string from the search form)pg
(the current page number)status
(document status: 200, 404, 403, etc)next_id
(ID of next document if on article page)next_title
(Title of next document if on article page)prev_id
prev_title
my_custom_field_name
(any custom fields you have defined)urlvar:my_variable_name
(any variable in the address bar)svrvar:my_variable_name
(any server variable, e.g. HTTP_USER_AGENT)If you specify a field name that does not exist, the text you use will be taken verbatim in most cases.
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:
eq
Equal (the default)not
Not equallt
Less thangt
Greater thanle
Less than or equal toge
Greater than or equal tobegins
Variable begins with a sequence of characterscontains
Variable contains a sequence of charactersisnum
Variable is a numberisempty
Variable is empty (contains nothing)isused
Variable has some valuedefined
Variable is set (useful with urlvar variables)undefined
Variable is not set, or missing from the URL lineWith the comparison operators (gt, lt, ge, le) you may find odd behaviour when comparing numbers. For example, urlvar:pic gt 6
, will return "true" if the pic
variable is set to "fred". This is because the word 'fred' (or at least the "character f") is greater in the ASCII table than the "character 6".
To circumvent this problem, you may append :NUM
to the end of any of these 4 operators to force the plugin to check that the values are integers.
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.
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.
Example 5
<txp:smd_if field="urlvar:pic, urlvar:hide" operator="gt:NUM, undefined" value="5,"> <txp:article> <txp:else /> <p>Not today, thanks</p> </txp:smd_if>
Tests if the url variable pic
is strictly numerically greater than 5 and that the url variable hide
is missing from the URL address.