Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#301 2010-02-19 17:52:27

mlarino
Member
Registered: 2007-06-29
Posts: 367

Re: smd_if: Generic multiple if condition tests

Thanks Stef!
it was the line break problem.
Great plugin!!! helping a lot on this realestate website I am making!

Offline

#302 2010-03-02 02:10:33

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

Re: smd_if: Generic multiple if condition tests

Hiya Stef,

I have the following code:

<txp:smd_if field="urlvar:day" operator="gt:NUM, lt:NUM" value="0, 32">
  <txp:smd_if field="urlvar:month" operator="gt:NUM, lt:NUM" value="0, 13">
    something fantastic happens here!
  </txp:smd_if>
</txp:smd_if>

and was wondering whether it would be possible to create a new operator called bn (to stand for “between”) where the numerical values to compare could be separated by “|”? The above code could then be written as below:

<txp:smd_if field="urlvar:day, urlvar:month" operator="bn:NUM, bn:NUM" value="0|32, 0|13">
  something fantastic happens here!
</txp:smd_if>

What say you, kind Sir?


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

Offline

#303 2010-03-02 08:43:27

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

Re: smd_if: Generic multiple if condition tests

speeke wrote:

create a new operator called bn (to stand for “between”) where the numerical values to compare could be separated by “|”?

A noble request. Leave it with me and I’ll see if the plugin can be beaten into this shape for you.


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

#304 2010-03-02 09:50:18

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

Re: smd_if: Generic multiple if condition tests

Done :-)

Two new operators for your comfort and enjoyment:

  • between for testing if the given values lie (exclusively) between the values you give
  • range is exactly the same but includes the end points as valid values

In other words, between uses > and <, whereas range uses >= and <= (the :NUM is optional for most cases). Note that you use the list_delim attribute between values, e.g. value="0/32".

See example 10 in the help for an embodiment of your post above. As always, let me know how y’all get on with this version.


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

#305 2010-03-02 11:12:07

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

Re: smd_if: Generic multiple if condition tests

Brilliant Stef!

Shall take it for a whirl and let you know how it goes. Thanks so much :-)

BTW: Your mention of 31st February in Example 10 has given me a bit more to think about, like 31st September/April/June/November! However, in my case, I will be testing user input for day, month and year as it relates to the user’s date of birth. I guess if someone wants to record their birthday as 31 September 2011, then good luck to them ;-)


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

Offline

#306 2010-03-02 11:25:27

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

Re: smd_if: Generic multiple if condition tests

Just tested the following:

<txp:php>
global $thisyear;
$thisyear = date("Y");
</txp:php>
<txp:smd_if field="urlvar:d, urlvar:m, urlvar:y" operator="range, range, range" value="1/31, 1/12, 1900/phpvar:thisyear">
  blah, blah, blah
</txp:smd_if>

All works like a charm, except the global variable $thisyear – which worked with the previous version of the plugin. I’m guessing the list_delim attribute is causing confusion???


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

Offline

#307 2010-03-02 11:46:53

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

Re: smd_if: Generic multiple if condition tests

speeke wrote:

except the global variable $thisyear

Bugger, more thought required. All our lines are currently busy but your call is important to us. Please hold and an operator will be with you shortly…


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

#308 2010-03-02 23:14:09

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

Re: smd_if: Generic multiple if condition tests

Sheesh, that was harder than I expected. After much fiddling and large-scale internal code refactorisation, smd_if is back with a brand new pig bag

From your perspective, there’s nothing new in this version except that you now have the capability to specify more than one field inside values. For example:

<txp:smd_if field="urlvar:tester" operator="in" value="1/3/7/?custom1/13/txpvar:some_val/19">
   // tester matches one of the values
</txp:smd_if>

In previous versions that would fail, but the plugin now correctly inserts each value — or sub-list of values! — in place of its variable and then the whole list is compared with the field. This works using operators in, notin, between and range. You’ll also get a whole load more replacement tags with these operators — one for each value in your list. That gives you more flexibility about what to display inside your container.

Because of the big changes inside the plugin I’ve bumped the major version number. This version should however be backwards compatible with previous versions; please let me know if I’ve missed anything or broken any previous smd_if magic.

Above all, enjoy.

Last edited by Bloke (2010-03-02 23:17:29)


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

#309 2010-03-02 23:31:57

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

Re: smd_if: Generic multiple if condition tests

Bloke wrote:

After much fiddling and large-scale internal code refactorisation, smd_if is back with a brand new pig bag.

Thanks, Mr Incredible!

please let me know if I’ve missed anything or broken any previous smd_if magic.

Will do.

Above all, enjoy.

Absolutely … as always ;-)


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

Offline

#310 2010-03-03 01:59:10

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

Re: smd_if: Generic multiple if condition tests

OK, so I have the thrown together the code below to check for invalid date combos (following on from your note in Example 10):

<txp:smd_if field="urlvar:d, urlvar:m" operator="eq, in" value="31, 2/4/6/9/11">
  <txp:variable name="datetest" value="0" />
</txp:smd_if>
<txp:smd_if field="urlvar:d, urlvar:m" operator="eq, eq" value="30, 2">
  <txp:variable name="datetest" value="0" />
</txp:smd_if>
<txp:smd_if field="urlvar:d, urlvar:m, phpvar:leaptest" operator="eq, eq, not" value="29, 2, 1">
  <txp:variable name="datetest" value="0" />
</txp:smd_if>

The first smd_if checks for months that don’t have day 31, the next checks for months that don’t have day 30 (Feb), and the last checks 29 Feb for leap years (the php variable leaptest is set to 1 if it is a leap year).

I tried placing the php function that tests for a leap year in the smd_if field, but it spat the dummy.

The TxP variable datetest is used to determine if an error message should be shown.

So this is the “prettiest” approach I could come up with. Am I using the plugin in the best possible configuration here?


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

Offline

#311 2010-03-03 12:18:30

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,719
Website

Re: smd_if: Generic multiple if condition tests

I’m using smd_if within smd_each to customise the separator much like the example that finds the last item except that I want the last separator which equates to the penultimate item. I tried comparing with {smd_var_total}-1 but no dice:

<txp:smd_if field="{smd_var_counter}" operator="eq" value="{smd_var_total}-1">This is the last item</txp:smd_if>

How would I get the penultimate item? The desired outcome is “this, that, the other and one extra”, e.g. a comma as normal separator but an “and” for the last separator. Or is there a better way of achieving this with smd_each?


TXP Builders – finely-crafted code, design and txp

Offline

#312 2010-03-03 13:55:25

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,719
Website

Re: smd_if: Generic multiple if condition tests

Using value='<txp:php>echo {smd_var_total}-1;</txp:php>' does it :-)

Summary: to get “link-a, link-b, link-c and link-d” out of a comma-separated list of article-ids in my-custom-field e.g. “12,22,45,32” use:

<txp:smd_each include="my-custom-field" subset="2">
   <txp:permlink id="{smd_var_value}" />
   <txp:smd_if field="{smd_var_counter}" operator="not,not" value='{smd_var_total},<txp:php>echo {smd_var_total}-1;</txp:php>'>, </txp:smd_if>
   <txp:smd_if field="{smd_var_counter}" operator="eq" value='<txp:php>echo {smd_var_total}-1;</txp:php>'> and </txp:smd_if>
</txp:smd_each>

(line wrapping is for better readability but causes extra spaces before the commas. Run the tags together without line breaks to obtain clean output).

I guess adi_calc and setting a variable would work here too. But perhaps there’s a better way altogether?

Last edited by jakob (2010-03-03 14:11:32)


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB