Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2024-04-04 16:39:09

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

Variable tweaks

Sometimes we need to use the same tag multiple times, e.g.

<!-- Summertime -->
<txp:evaluate query='<txp:date format="%m" escape="integer" /> >= 6 and <txp:date format="%m" escape="integer" /> <= 8'>
The living is easy!
</txp:evaluate>

In this case it is more computationally economic to store the tag’s output in a variable first:

<!-- Summertime -->
<txp:variable name="month" value='<txp:date format="%m" escape="integer" />' />
<txp:evaluate query='<txp:variable name="month" /> >= 6 and <txp:variable name="month" /> <= 8'>
The living is easy!
</txp:evaluate>

This is not very readable. What if it could be reduced to something like

<!-- Summertime -->
<txp:date format="%m" escape="integer" variable="month" />
<txp:evaluate query="$month >= 6 and $month <= 8" import="month">
The living is easy!
</txp:evaluate>

by introducing a global variable attribute (import to be discussed)?

Offline

#2 2024-04-04 17:26:01

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

Re: Variable tweaks

My instinct says: Holy Yes Cakes!

My only question is over the naming. Does the variable="somevar" attribute actually store the result in the given Txp variable so it can be extracted using <txp:variable name="somevar" /> afterwards? In which case, that’s a brilliant additional feature and a great name for it. It could open up some other handy usages too.

If not, maybe something like export would match the companion import to read it in? Not given the naming much thought beyond that so there might be even better names out there. But I love the direction.

EDIT: also, is $ the best way to access the variable from within other attributes? Could that clash with something? Would a different txpish prefix be more robust?

Last edited by Bloke (2024-04-04 17:28:04)


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

#3 2024-04-04 17:41:47

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

Re: Variable tweaks

Thanks!

Bloke wrote #337021:

Does the variable="somevar" attribute actually store the result in the given Txp variable so it can be extracted using <txp:variable name="somevar" /> afterwards?

Yep, that’s the idea.

If not, maybe something like export would match the companion import to read it in?

This is another possibility, are there any extra benefits in it?

EDIT: also, is $ the best way to access the variable from within other attributes? Could that clash with something? Would a different txpish prefix be more robust?

I’ve actually only thought of query attribute of <txp:evaluate /> tag (and maybe few more). Do you see other attributes susceptible to use variables massively? I mean, blindly replacing patterns in every attribute is expensive..

Offline

#4 2024-04-04 17:48:26

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

Re: Variable tweaks

We surely could find a way to indicate which variables and in which attributes we need to import, like

query: month, year | thing: year

but it looks a bit overkill.

Anyway, global variable attribute looks handy, so I’ve pushed it to dev (along with $somevar substitutions in evaluate, for testing).

Edit: sorry, it is called alias instead of import in my commit, subject to change.

Offline

#5 2024-04-04 18:27:46

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

Re: Variable tweaks

Not thought the extended ramifications or use cases through. If its main application is queries then that’s fine. Dollars just seem a bit programmery is all.


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

#6 2024-04-04 18:29:00

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

Re: Variable tweaks

Alias is fine. Alternatives:

  • Use
  • Get
  • Read
  • Grab

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

#7 2024-04-05 04:08:02

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,599
GitHub Twitter

Re: Variable tweaks

… As feedback:

I just noticed an infinite loop within a blank page 😱 on a fairly complex new project I’m working on…

Undoing your last commit restores a very fast TXP CMS (4.9-dev)… (however, I’m loving these latest features you introduced, especially the new <txp:date />).

Last edited by Pat64 (2024-04-05 04:12:07)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#8 2024-04-05 06:50:21

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

Re: Variable tweaks

Interesting. Are you using variable attribute, per se or as a parameter of some shortcode? Mind posting the relevant code?

Offline

#9 2024-04-05 16:11:55

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,599
GitHub Twitter

Re: Variable tweaks

Just TXP variables (a lot) and custom fields (a lot, too).


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#10 2024-04-05 16:50:49

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

Re: Variable tweaks

This does not help me much, but please try the latest commit, just in case.

Offline

#11 2024-04-06 01:57:09

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,081
Website

Re: Variable tweaks

Perhaps related: I had the following at the start of shortcode form that is inserted inside the body (<txp:body />) of an article:

<txp:evaluate query='starts-with( "<txp:page_url/>", "/rss")' escape='trim,textile'>

Up till now this worked fine (did not cause any error), but now I get the following error message when viewing an article containing that shortcode:

Tag error: <txp:page_url/> ->  Textpattern Warning: page_url/ tag does not exist

Looking closely, there is a missing space between _url and /. Once added the error goes away.

TXP 4.9-dev latest (testing mode), PHP 8.2


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

#12 2024-04-06 02:01:17

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,081
Website

Re: Variable tweaks

BTW, on the main topic, this will be a valuable addition I think. And alias sound good.


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

Board footer

Powered by FluxBB