Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Yesterday 18:04:04

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,553
Bitbucket GitHub

Numeric variables, addition & subtraction

I have a numeric variable, and I’d like to do some addition / subtraction calculations on it to either a) create / update a different variable, or just output the result of the calculation.

Is such a thing possible with native tags, please? I’ve checked docs and not struck gold as yet, but since this is my first ever time using variables with calculations, I may well be missing something fundamental.

Thank you.

Caveat: this might be a job for etc_query, but I figured I’d ask about the native approach.

Offline

#2 Yesterday 18:15:46

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,031
Website GitHub

Re: Numeric variables, addition & subtraction

<txp:variable name="my_counter" add /> will increment a value by 1. Use add="5" to change the positive step, or negative numbers to subtract values each time the tag is encountered.

Wrap it in another <txp:variable> tag to update a different variable. You can use the output attribute to display/return the current value.

For anything more, plug your variable tags into <txp:evaluate> to do more complex stuff.

The docs are woefully lacking, oops…

Last edited by Bloke (Yesterday 18:17:50)


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 Yesterday 18:21:24

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,553
Bitbucket GitHub

Re: Numeric variables, addition & subtraction

Bloke wrote #341062:

The docs are woefully lacking, oops…

My grey matter is somewhat lacking, too…I think a few hours of finagling is in order, and I’ll share the results when (if) they work.

Offline

#4 Yesterday 18:38:20

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,031
Website GitHub

Re: Numeric variables, addition & subtraction

Actually, I just checked, and there are some good examples on the txp:variable tag doc page that might give you some ideas of usage.


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

#5 Yesterday 20:55:56

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,553
Bitbucket GitHub

Re: Numeric variables, addition & subtraction

Against considerable (brain-related) odds, I’ve got a working scaffold for the following conditions:

  • Get the current day of the year with PHP; increment by one since years start on day 0 in PHP.
  • Block out two time periods of the year: the six months around the shortest day is ‘Dark’, and the six months around the longest day is ‘Light’.
  • If the current day is ‘Dark’ find the number of days until ‘Light’, and vice versa.

…and it works! My first use of variable with evaluate has a check in its box.

Leap year stuff is beyond scope for now, but I’ve got some time to figure that out.

Offline

#6 Yesterday 21:33:54

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,031
Website GitHub

Re: Numeric variables, addition & subtraction

Nice work!

For cleanliness, you should be able to remove the PHP block and use the native <txp:date> tag in 4.9.0.


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 Yesterday 22:07:02

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,553
Bitbucket GitHub

Re: Numeric variables, addition & subtraction

Bloke wrote #341066:

For cleanliness, you should be able to remove the PHP block and use the native <txp:date> tag in 4.9.0.

Jackpot. Thank you very much.

Offline

Board footer

Powered by FluxBB