Textpattern Forum

You are not logged in. Register | Login | Help

#221 2009-03-22 22:37:21

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,936
Website

Re: smd_if: Generic multiple if condition tests

gomedia wrote:

a trim="1" attribute

I didn’t do it that way in case you only wanted to apply it to one particular test — it’s more granular when comparing multiple, disparate things. But offering the option to globally apply it makes sense so I’ll do that as well to save you the hassle if you need to trim all fields+values in all tests.

What I might actually do is offer a modifier attribute, where you can choose which modifiers to apply across the board. e.g. modifier="TRIM, NUM" would trim all fields and values in all tests, and make sure any numeric-based operations were treated as numbers instead of strings. In theory I think it’ll work; need to think it through to check it’s feasible and you can’t inadvertently create some paradox that defies the space-time continuum.

Not tonight though. Eyes closing…


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

#222 2009-03-26 12:45:45

rathersplendid
Plugin Author
From: London
Registered: 2008-05-02
Posts: 163
Website

Re: smd_if: Generic multiple if condition tests

Hey Stef, or anyone else who maybe able to help out,

I’m looking to, hopefully, use this plugin to create a conditional to work out the current month/date/time and change the page’s body class based on the result. What I want to do is use this to the change the page layout using CSS. Do you see what I mean?

Do you think this can be done? And if so, how?? My PHP is not pretty poor…

Thanks in advance guys.


Admin Themes Prometheus | Stung | txp:coder/
My Portfolio | ɹǝpuɐz.com | @MrMartineau
txp:coder /> – Convert your designs into Textpattern-based websites
jQuery Style | @jquerystyle

Offline

#223 2009-03-26 13:08:34

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,936
Website

Re: smd_if: Generic multiple if condition tests

rathersplendid wrote:

create a conditional to work out the current month/date/time and change the page’s body class based on the result.

It kind of depends how clever you want to be. Are you going to want to read the date from anywhere, like the URL? (e.g. site.com/2009/03/26?) Or do you just care about changing the look based on “now” (and is “now” the user’s “now” or the server’s “now” because it may make a difference?)

smd_calendar has some supoprt for getting today’s date through the <txp:smd_cal_now /> tag but it’s probably less hassle to do something directly, like this:

<txp:variable name="theDay" value='<txp:php>echo strftime("%d");</txp:php>' />
<txp:variable name="theMonth" value='<txp:php>echo strftime("%m");</txp:php>' />
<txp:variable name="theYear" value='<txp:php>echo strftime("%Y");</txp:php>' />
<txp:variable name="theTime" value='<txp:php>echo strftime("%T");</txp:php>' />

and then use smd_if with txpvar: to do some basic checking like is theMonth > 6 and so on. Look at strftime for some ideas of how you can use it to get at the current bits of the current date/time to build your own output.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

#224 2009-03-26 13:13:22

rathersplendid
Plugin Author
From: London
Registered: 2008-05-02
Posts: 163
Website

Re: smd_if: Generic multiple if condition tests

Stef, it needs to be now, and it doesn’t really matter for me about server time or user time because, for the moment at least, I only need to change the style by month.

I will have a look at what you suggested and get back to you; from the looks of it, I should be able to manage it now.

Cheers mate


Admin Themes Prometheus | Stung | txp:coder/
My Portfolio | ɹǝpuɐz.com | @MrMartineau
txp:coder /> – Convert your designs into Textpattern-based websites
jQuery Style | @jquerystyle

Offline

#225 2009-04-04 23:05:10

maniqui
Moderator
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 2,989
Website

Re: smd_if: Generic multiple if condition tests

Hi Bloke,
For testing purposes, I’ve wrote this code:

<txp:smd_if logic="OR" field="urlvar:jquery" operator="eq,eq,eq" value="1.2.6,1.3.1,1.3.2">
  <script src="/js/jquery-{smd_if_jquery}.min.js" type="text/javascript"></script>
  <txp:else />
  <script src="/js/jquery-1.2.6.min.js" type="text/javascript"></script>
</txp:smd_if>

Wonder if it makes senses to have some kind of “default” (fallback) value for undefined and similar operators.
So, to have the ability to write something like this:

<txp:smd_if logic="OR" field="urlvar:jquery" operator="eq,eq,eq,undefined" value="1.2.6,1.3.1,1.3.2,1.2.6">
  <script src="/js/jquery-{smd_if_jquery}.min.js" type="text/javascript"></script>
</txp:smd_if>

There, the fallback value for undefined is the latest 1.2.6.

Last edited by maniqui (2009-04-04 23:11:12)


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#226 2009-04-04 23:14:52

maniqui
Moderator
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 2,989
Website

Re: smd_if: Generic multiple if condition tests

BTW, the help docs says:

Note that, although the first three attributes are considered mandatory, if you happen to require the default operator for all your tests, you can safely omit operator.

I’ve tried (no explicit operator attribute):

<txp:smd_if logic="OR" field="urlvar:jquery" value="1.2.6,1.3.1,1.3.2">
  <script src="/js/jquery-{smd_if_jquery}.min.js" type="text/javascript"></script>
  <txp:else />
  <script src="/js/jquery-1.2.6.min.js" type="text/javascript"></script>
</txp:smd_if>

But it always seem to enter the txp:else branch.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#227 2009-04-04 23:15:40

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,936
Website

Re: smd_if: Generic multiple if condition tests

maniqui wrote:

Wonder if it makes senses to have some kind of “default” (fallback) value

Interesting. I put this concept in smd_query so I guess there’s no reason it can’t migrate its way across here too.

Let me have a think about how to do it and see if I can make it work in some reasonable (and extensible, semi-logical) fashion for relevant operators. Thanks for the idea.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

#228 2009-04-04 23:39:28

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,936
Website

Re: smd_if: Generic multiple if condition tests

maniqui wrote:

<txp:smd_if logic="OR" field="urlvar:jquery" value="1.2.6,1.3.1,1.3.2">

Right, the docs are slightly misleading here *ahem*. The short-circuit system is actually only based on the field and operator not value. It only checks the number of items in the greater of either field or operator. So in your case, you have 1 field and 1 (default) operator, so it only checks 1 value. If you had:

<txp:smd_if logic="OR" field="urlvar:jquery" operator="gt, lt" value="1.2.6,1.3.1,1.3.2">

it would check ‘jquery’ against the first two values (because there are two operators). If you had:

<txp:smd_if logic="OR" field="urlvar:jquery, urlvar:othervar" operator="gt" value="1.2.6,1.3.1,1.3.2">

it would check if ‘jquery’ is greater than 1.2.6 and if ‘othervar’ is greater than 1.3.1 and then stop. So the value attribute is really a 2nd class citizen; the number of tests performed are only as great as the number of operators or fields. I couldn’t find a very good way of explaining that in the docs, but perhaps I should come up with something!

I did try and make it work so you could short circuit values too — as you expect, here — but it kept giving me runtime errors due to missing content, so I gave up. Perhaps I’ll find a way one day.

Oh, and incidentally, could you do this instead to simplify things:

<txp:smd_if field="urlvar:jquery" operator="in" value="1.2.6/1.3.1/1.3.2">
...

The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

#229 2009-04-05 00:05:29

maniqui
Moderator
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 2,989
Website

Re: smd_if: Generic multiple if condition tests

As always, thanks for the quick enlightenment, sir!
This plugin (as most of the smd_ demons —see below—) is so f* powerful.

And I’ve to admit that there was a time when I was afraid of using Bloke’s plugins… I’ve even asked at #xpat or #textpattern if I should use them or if I should keep away from them.
Why? Because, at that time, I’ve read somewhere something (wrote by you, I think) about the eval/evil thing…
Of course, I’ve no idea about what that could really mean. But it was scary

But as today, smd_ is my new religion, and you are the God of TXP plugins.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#230 2009-04-05 00:30:28

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,936
Website

Re: smd_if: Generic multiple if condition tests

maniqui wrote:

I’ve read somewhere something (wrote by you, I think) about the eval/evil thing…

Yeah, it’s in the docs :-)

I don’t mind eval() so much because it’s a tool that is there to be used (TXP uses it in the core). But I know that some people regard it as the devil’s elixir, and if one uses it then one is a lesser programmer because it’s “cheating” or it’s “unsafe”. Well, in this instance it is perhaps cheating, but it works.

In the next major release I’d like to do the ‘default’ thing you mentioned, and also address security by putting in some stuff that I learned from writing v0.2 of smd_query. That should help make the plugin a lot safer from malicious intent, by virtue of throwing the ball in your court!

As it stands you’ve gotta be going some to get past it, because it uses a templating system, plugs your values into the template and has a special security “feature” (an undocumented, but intentionally untrapped ‘bug’) whereby if you give it extraneous quotes or weird characters, the plugin blows up and stops executing, thus protecting you from attack. It’s the programming equivalent of curling up into a ball and whimpering when someone kicks you in the face ;-)

But as today, smd_ is my new religion, and you are the God of TXP plugins.

Too kind. But thank you. Glad to be of service.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB