Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-10-24 18:08:37

etc
Developer
Registered: 2010-11-11
Posts: 5,145
Website GitHub

Textpattern evaluation (dis)abilities

I often miss the ability to evaluate simple arithmetic (like 2*2), logical (contains('<txp:title />', 'etc')) and other expressions in Textpattern. Certainly, there is <txp:php /> and plugins, but I would like to introduce a tag for this. So, please test <txp:evaluate /> branch and suggest any ideas before it goes (or not) into 4.7. The usage is simple:

<txp:evaluate query="2*2" /> (outputs 4)

<txp:evaluate query="starts-with('Textpattern', 'Text') and not(contains('Textpattern', 'press'))">
yay
<txp:else />
nay
</txp:evaluate>

See here for the list of available functions. Site admins can extend (via a pref) this list with PHP string functions that they consider safe to use.

Edit: another example: increment the value of a variable (e.g. on each loop) and compare it to something:

<txp:variable name="counter" value="0" />
...
<txp:variable name="counter" value='<txp:evaluate query=''<txp:variable name="counter" /> + 1'' />' />
<txp:evaluate query='<txp:variable name="counter" /> >= 5'>
    you'd better stop
</txp:evaluate>

Last edited by etc (2016-10-24 20:51:47)

Offline

#2 2016-10-25 14:16:26

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,306

Re: Textpattern evaluation (dis)abilities

Yes! Definitely! Please!

At the time the idea of variables was presented I hinted at the need of an evaluation method for variables. In the meantime two variable evaluation plugins plus Ruuds var hack for his rvm_counter confirmed that need.

And now core tags will take care of that: Great! Even better that you intend to add text evaluations! That’s not covered yet by plugins AFAIR! Both of these features together matches the Textpattern way of versatility and opens a lot of possibilities, and I’m totally looking forward on having these at my disposal, yay!

My only concern is that you seem to introduce another syntax, apparently different from the if_custom_field’s match="pattern" method. Or am I currently being too dim? (PS: Looking at the function list it might be XPath?)

Unfortunately I’m currently quite overwhelmed with work so I can’t test yet, I’m sorry. (PS 2: And if it’s XPath I’d be stretched too thin anyway.)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#3 2016-10-25 14:21:05

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,306

Re: Textpattern evaluation (dis)abilities

BTW, if the query value here

<txp:variable name="counter" value='<txp:evaluate query=''<txp:variable name="counter" /> + 1'' />' />

contains tags in tags: Would that be possible by wrapping three single apostrophes (''') around?


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#4 2016-10-25 16:18:06

etc
Developer
Registered: 2010-11-11
Posts: 5,145
Website GitHub

Re: Textpattern evaluation (dis)abilities

uli wrote #302477:

My only concern is that you seem to introduce another syntax, apparently different from the if_custom_field’s match="pattern" method.

Yes, it’s a different thing. You can use any function allowed by site admins in evaluations. Few of them are predefined (see below), but the list is extendible. If you wish, you could set $prefs['txp_functions'] = 'date,preg_match' and call

<txp:evaluate query="date('w')=0 or date('w')=6">week-end!</txp:evaluate>

or

<txp:evaluate query='preg_match("/\bParis\b/", ''<txp:custom_field name="cities" />'')' />

(PS: Looking at the function list it might be XPath?)

Damn, I’m discovered! :-) Yes, because it’s simple, fast and secure to use. There are only few predefined constructs (arithmetic, logic, some functions), but you (admin) can extend the list at your liking (see above).

Offline

#5 2016-10-25 16:19:42

etc
Developer
Registered: 2010-11-11
Posts: 5,145
Website GitHub

Re: Textpattern evaluation (dis)abilities

uli wrote #302478:

Would that be possible by wrapping three single apostrophes (''') around?

As much as you like, every pass removes one quote.

Offline

#6 2016-10-25 22:57:19

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,306

Re: Textpattern evaluation (dis)abilities

etc wrote #302482:

<txp:evaluate query='preg_match("/\bParis\b/", ''<txp:custom_field name="cities" />'')' />...

The preg_match option is reassuring (my POV).

And $prefs['txp_functions'] = 'date,preg_match' would go into config.php?


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#7 2016-10-25 23:07:01

etc
Developer
Registered: 2010-11-11
Posts: 5,145
Website GitHub

Re: Textpattern evaluation (dis)abilities

uli wrote #302492:

And $prefs['txp_functions'] = 'date,preg_match' would go into config.php?

I would create an empty pref that admins can adjust as they want in Admin/Preferences tab (not done yet).

Offline

#8 2016-10-27 07:40:49

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,373

Re: Textpattern evaluation (dis)abilities

At the risk of starting a bun fight (or causing offence) … this looks great for a plugin but built into the core, not so sure.

Textpattern lacks basic built-in website building functionality at the moment – whitespace trimming, intelligent wrap tagging, standard tags (e.g. txp:custom_field) missing wrap tag attributes, access to URL vars etc etc – I’d hope that the core priorities would be elsewhere.

I think adi_if_content (written by etc) is a better & more powerful candidate for core inclusion.

And the syntax of txp:evaluate would be borderline impenetrable to many users – destroying one of Textpatterns strengths: code readability.

Offline

#9 2016-10-27 11:11:54

etc
Developer
Registered: 2010-11-11
Posts: 5,145
Website GitHub

Re: Textpattern evaluation (dis)abilities

gomedia wrote:

At the risk of starting a bun fight (or causing offence) … this looks great for a plugin but built into the core, not so sure.

Absolutely no offense taken nor intended — all ideas are welcome.

Textpattern lacks basic built-in website building functionality at the moment – whitespace trimming, intelligent wrap tagging, standard tags (e.g. txp:custom_field) missing wrap tag attributes, access to URL vars etc etc – I’d hope that the core priorities would be elsewhere.

True, and we are working on it. I have already tested “core” attributes, like trim or wrap. It works, we just need a distinctive name: _trim, txp:trim or something else. Any idea?

I think adi_if_content (written by etc) is a better & more powerful candidate for core inclusion.

Possible. What about enhancing <txp:hide /> with adi_if_content functionality?

And the syntax of txp:evaluate would be borderline impenetrable to many users – destroying one of Textpatterns strengths: code readability.

Here I must disagree: how <txp:evaluate query="2*2=4" /> is less readable than

<txp:adi_calc name="test" value="2" multiply="2" />
<txp:if_variable name="test" value="4" />

Offline

#10 2016-10-27 11:25:14

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Textpattern evaluation (dis)abilities

etc wrote #302513:

Absolutely no offense taken nor intended — all ideas are welcome.

True, and we are working on it. I have already tested “core” attributes, like trim or wrap. It works, we just need a distinctive name: _trim, txp:trim or something else. Any idea?

Just a word about that; on my side I’d prefer to keep txp: for tags and use something else for attributes; underscore seems ok to me, and it is three characters less! ;-)

I still need to test txp:evaluate


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#11 2016-10-27 11:30:20

etc
Developer
Registered: 2010-11-11
Posts: 5,145
Website GitHub

Re: Textpattern evaluation (dis)abilities

NicolasGraph wrote #302514:

Just a word about that; on my side I’d prefer to keep txp: for tags and use something else for attributes; underscore seems ok to me, and it is three characters less! ;-)

+1

Offline

#12 2016-10-27 11:42:15

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,373

Re: Textpattern evaluation (dis)abilities

etc wrote #302513:

Here I must disagree: how <txp:evaluate query="2*2=4" /> is less readable than

<txp:adi_calc name="test" value="2" multiply="2" />...

Poor little adi_calc – it’s only a simple little sausage! To me that says very loudly: perform a calculation on a variable and then test the value of the variable.

If txp:evaluate is to become a general purpose conditional maybe it should be txp:if.

Offline

Board footer

Powered by FluxBB