Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Yesterday 18:54:19

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

Correct syntax for variable output multiple times

I’m calculating a variable from another variable & evaluate, like this:

<txp:variable name="days_into_dark" output="1" value='<txp:evaluate query='<txp:variable name="day" /> - 264' />' />

This works in so far as it outputs the correct number. All good there. I cannot output that variable an additional time, including immediately afterwards.

Given the working code above, I expected the following to output the variable twice:

<txp:variable name="days_into_dark" output="1" value='<txp:evaluate query='<txp:variable name="day" /> - 264' />' />
<txp:variable name="days_into_dark" output="1" />

The second line has no effect, and I’m confused as to why not. What am I doing wrong?

Thank you.

Offline

#2 Yesterday 19:40:40

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

Re: Correct syntax for variable output multiple times

Try the second one without the output attribute? That usually spits the value out for me.


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 19:49:46

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

Re: Correct syntax for variable output multiple times

Bloke wrote #341076:

Try the second one without the output attribute? That usually spits the value out for me.

Thanks, Bloke. No dice. Same result. I’m a bit baffled.

Here’s the whole scaffold:

<txp:variable name="day" trim="1">
  <txp:date time="now" format="%j" />
</txp:variable>
<txp:evaluate query='<txp:variable name="day" /> > 80 and <txp:variable name="day" /> < 264'>
  Days until Dark: <txp:variable name="days_until_dark" output="1" value='<txp:evaluate query='264 - <txp:variable name="day" />' />' />
  Days into Light:
<txp:else />
  Days until Light:
  <txp:evaluate query='<txp:variable name="day" /> < 80'>
	<txp:variable name="days_until_light" output="1" value='<txp:evaluate query='80 - <txp:variable name="day" />' />' />
  <txp:else />
	<txp:variable name="days_until_light" output="1" value='<txp:evaluate query='365 + 80 - <txp:variable name="day" />' />' />
  </txp:evaluate>
  Days into Dark:
  <txp:evaluate query='<txp:variable name="day" /> < 80'>
	<txp:variable name="days_into_dark" output="1" value='<txp:evaluate query='101 + <txp:variable name="day" />' />' />
	<txp:variable name="days_into_dark" />
  <txp:else />
	<txp:variable name="days_into_dark" output="1" value='<txp:evaluate query='<txp:variable name="day" /> - 264' />' />
	<txp:variable name="days_into_dark" />
  </txp:evaluate>
</txp:evaluate>

I’ll grab a tag trace for the next post.

Offline

#4 Yesterday 19:58:52

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

Re: Correct syntax for variable output multiple times

Tag trace excerpt:

	 28.16 |     0.09 | 	<txp:evaluate query='<txp:variable name="day" /> > 80 and <txp:variable name="day" /> < 264'>
	 28.16 |     0.02 | 		[attribute 'query']
	 28.16 |          | 			[true]
	 28.17 |     0.01 | 			<txp:variable name="day" />
	 28.18 |     0.00 | 			<txp:variable name="day" />
	 28.18 |          | 		[/attribute]
	 28.24 |          | 		[false]
	 28.25 |          | 	</txp:evaluate>
	 28.25 |     0.11 | 	<txp:evaluate query='<txp:variable name="day" /> > 80 and <txp:variable name="day" /> < 264'>
	 28.25 |     0.01 | 		[attribute 'query']
	 28.25 |          | 			[true]
	 28.25 |     0.00 | 			<txp:variable name="day" />
	 28.26 |     0.00 | 			<txp:variable name="day" />
	 28.26 |          | 		[/attribute]
	 28.27 |          | 		[false]
	 28.27 |     0.05 | 		<txp:evaluate query='<txp:variable name="day" /> < 80'>
	 28.28 |     0.01 | 			[attribute 'query']
	 28.28 |          | 				[true]
	 28.28 |     0.00 | 				<txp:variable name="day" />
	 28.29 |          | 			[/attribute]
	 28.29 |          | 			[false]
	 28.29 |     0.02 | 			<txp:evaluate query='365 + 80 - <txp:variable name="day" />' />
	 28.30 |     0.01 | 				[attribute 'query']
	 28.30 |          | 					[true]
	 28.30 |     0.00 | 					<txp:variable name="day" />
	 28.31 |          | 				[/attribute]
	 28.32 |          | 		</txp:evaluate>
	 28.32 |     0.04 | 		<txp:evaluate query='<txp:variable name="day" /> < 80'>
	 28.32 |     0.01 | 			[attribute 'query']
	 28.32 |          | 				[true]
	 28.32 |     0.00 | 				<txp:variable name="day" />
	 28.33 |          | 			[/attribute]
	 28.33 |          | 			[false]
	 28.33 |     0.02 | 			<txp:evaluate query='<txp:variable name="day" /> - 264' />
	 28.34 |     0.01 | 				[attribute 'query']
	 28.34 |          | 					[true]
	 28.34 |     0.00 | 					<txp:variable name="day" />
	 28.35 |          | 				[/attribute]
	 28.35 |     0.01 | 			<txp:variable name="days_into_dark" />
	 28.36 |          | 				[<txp:variable>: Unknown variable 'days_into_dark']
	 28.36 |          | 		</txp:evaluate>
	 28.36 |          | 	</txp:evaluate>
	 28.36 |     0.01 | 	<txp:variable name='days_into_dark' output='1' />
	 28.37 |     0.01 | 	<txp:variable name="days_into_dark" output="1" />
	 28.38 |     0.01 | 	<txp:date time="now" format="%j" />

Offline

#5 Yesterday 20:13:26

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

Re: Correct syntax for variable output multiple times

Hmmm. I pasted your code in and got values for both:

Days until Light: 139 Days into Dark: 42

Is that not what you’re expecting?

Just a thing about the value attribute when setting a variable. To save apostrophe hell with tags in attributes, you can set the value via the container instead.


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 Yesterday 20:35:10

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

Re: Correct syntax for variable output multiple times

Bloke wrote #341079:

Hmmm. I pasted your code in and got values for both:

Days until Light: 139 Days into Dark: 42

Is that not what you’re expecting?

Not quite. These two lines:

<txp:variable name="days_into_dark" output="1" value='<txp:evaluate query='<txp:variable name="day" /> - 264' />' />
<txp:variable name="days_into_dark" />

…if I’m understanding correctly, the first line defines the variable & outputs it, and the second should just output it – but it doesn’t (with or without output). So the output should be:

Days until Light: 139 Days into Dark: 42 42

Just a thing about the value attribute when setting a variable. To save apostrophe hell with tags in attributes, you can set the value via the container instead.

Thank you. I’m on the case with this now, I’m stripping it down and factoring in leap years so I can set it and forget it.

Last edited by gaekwad (Yesterday 20:35:51)

Offline

#7 Yesterday 20:49:38

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

Re: Correct syntax for variable output multiple times

I can only assume that the output is immediately returning the value and not assigning it to the variable. That might be why.

If you remove the first output in the assignment then you should be able to display the value after.


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

#8 Yesterday 21:06:54

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

Re: Correct syntax for variable output multiple times

Bloke wrote #341081:

I can only assume that the output is immediately returning the value and not assigning it to the variable. That might be why.

If you remove the first output in the assignment then you should be able to display the value after.

That makes good sense – thank you, Bloke.

Offline

#9 Yesterday 21:50:49

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

Re: Correct syntax for variable output multiple times

You are bitten by the dreaded quotes-in-quotes issue, attenuated by the second pass:

<txp:variable name="day" trim="1">
  <txp:date time="now" format="%j" />
</txp:variable>
<txp:evaluate query='<txp:variable name="day" /> > 80 and <txp:variable name="day" /> < 264'>
  Days until Dark: <txp:variable name="days_until_dark" output="1" value='<txp:evaluate query=''264 - <txp:variable name="day" />'' />' />
  Days into Light:
<txp:else />
  Days until Light:
  <txp:evaluate query='<txp:variable name="day" /> < 80'>
	<txp:variable name="days_until_light" output="1" value='<txp:evaluate query=''80 - <txp:variable name="day" />'' />' />
  <txp:else />
	<txp:variable name="days_until_light" output="1" value='<txp:evaluate query=''365 + 80 - <txp:variable name="day" />'' />' />
  </txp:evaluate>
  Days into Dark:
  <txp:evaluate query='<txp:variable name="day" /> < 80'>
	<txp:variable name="days_into_dark" output="1" value='<txp:evaluate query=''101 + <txp:variable name="day" />'' />' />
	<txp:variable name="days_into_dark" />
  <txp:else />
	<txp:variable name="days_into_dark" output="1" value='<txp:evaluate query=''<txp:variable name="day" /> - 264'' />' />
	<txp:variable name="days_into_dark" />
  </txp:evaluate>
</txp:evaluate>

Offline

#10 Yesterday 23:25:15

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,046
Website GitHub

Re: Correct syntax for variable output multiple times

etc wrote #341083:

You are bitten by the dreaded quotes-in-quotes issue, attenuated by the second pass:

One way to simplify that is to use txp:variable as a container tag:

<txp:date time="now" format="%j" variable="day" />
<txp:evaluate query='<txp:variable name="day" /> > 80 and <txp:variable name="day" /> < 264'>
  Days until Dark: <txp:variable name="days_until_dark" output><txp:evaluate query='264 - <txp:variable name="day" />' /></txp:variable>
  Days into Light:
<txp:else />
  Days until Light:
  <txp:evaluate query='<txp:variable name="day" /> < 80'>
    <txp:variable name="days_until_light" output><txp:evaluate query='80 - <txp:variable name="day" />' /></txp:variable>
  <txp:else />
    <txp:variable name="days_until_light" output><txp:evaluate query='365 + 80 - <txp:variable name="day" />' /></txp:variable>
  </txp:evaluate>
  Days into Dark:
  <txp:evaluate query='<txp:variable name="day" /> < 80'>
    <txp:variable name="days_into_dark" output><txp:evaluate query='101 + <txp:variable name="day" />' /></txp:variable>
    <txp:variable name="days_into_dark" />
  <txp:else />
    <txp:variable name="days_into_dark" output><txp:evaluate query='<txp:variable name="day" /> - 264' /></txp:variable>
    <txp:variable name="days_into_dark" />
  </txp:evaluate>
</txp:evaluate>

and if you’re not hell-bent on outputting the first instance as well, then you can use the variable attribute immediately in txp:evaluate to assign the tag’s output to a variable:

<txp:date time="now" format="%j" variable="day" />
<txp:evaluate query='<txp:variable name="day" /> > 80 and <txp:variable name="day" /> < 264'>
  Days until Dark: <txp:evaluate variable="days_until_dark" query='264 - <txp:variable name="day" />' />
<txp:variable name="days_until_dark" />
  Days into Light:
<txp:else />
  Days until Light:
  <txp:evaluate query='<txp:variable name="day" /> < 80'>
    <txp:evaluate variable="days_until_light" query='80 - <txp:variable name="day" />' />
    <txp:variable name="days_until_light" />
  <txp:else />
    <txp:evaluate variable="days_until_light" query='365 + 80 - <txp:variable name="day" />' />
    <txp:variable name="days_until_light" />
  </txp:evaluate>
  Days into Dark:
  <txp:evaluate query='<txp:variable name="day" /> < 80'>
    <txp:evaluate variable="days_into_dark" query='101 + <txp:variable name="day" />' />
    <txp:variable name="days_into_dark" />
  <txp:else />
    <txp:evaluate variable="days_into_dark" query='<txp:variable name="day" /> - 264' />
    <txp:variable name="days_into_dark" />
  </txp:evaluate>
</txp:evaluate>

TXP Builders – finely-crafted code, design and txp

Offline

#11 Today 08:56:35

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

Re: Correct syntax for variable output multiple times

This should output what you need, including leap years:

<txp:date format="%j" variable="day" />
<txp:date format="%Y" variable="year" />
<txp:evaluate alias="year" query="$year mod 4 = 0 and $year mod 100 != 0 or $year mod 400 = 0" variable="leap" />
<txp:variable name="days" value="365" add='<txp:variable name="leap" />' />
<txp:variable name="dark" value="264" add='<txp:variable name="leap" />' />

<txp:evaluate alias="day, dark" query="$day > 80 and $day < $dark">
  Days until Dark: <txp:evaluate alias="day, dark" query="$dark - $day" />
  Days into Light: <txp:evaluate query='<txp:variable name="day" /> - 80' />
<txp:else />
  Days until Light: <txp:evaluate alias="day, days" query='(80 - $day + $days) mod $days' />
  Days into Dark: <txp:evaluate alias="day, days" query='(101 + $day) mod $days' />
</txp:evaluate>

Offline

#12 Today 12:14:38

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,046
Website GitHub

Re: Correct syntax for variable output multiple times

etc wrote #341085:

This should output what you need, including leap years

Hero points! I do things much like Pete, so it’s great to see how a programmer goes about it.

Also, the use of alias is new to me. Nice!


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB