Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[solved] variable and if_variable strangeness
My aim is to have something appear if the site status is live
in preferences. I’m using the status in a variable
tag, and the querying it with if_variable
.
Vanilla Textpattern 4.5.7, factory fresh. Simplified code follows:
<txp:variable name="site_prod_status" value="<txp:php>echo $GLOBALS['production_status'];</txp:php>" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><txp:variable name="site_prod_status" /></title>
</head>
<body>
<p>1. var value [<txp:variable name="site_prod_status" />]</p>
<txp:if_variable name="site_prod_status" value="live">
<p>2. var value if live [<txp:variable name="site_prod_status" />]</p>
<txp:else />
<p>3. var value is not live</p>
</txp:if_variable>
</body>
</html>
The title is live
and the value at bullet point 1 is live
, so I’m working on the basis that the variable
tag is set correctly.
Bullet point 2 should appear if the variable is live
(which it is), but it doesn’t. Bullet point 3 should appear if the variable is not live
(which it isn’t), but it does. Both of these behaviours are counter to what they should be.
I’ve explained all this to my rubber duck and I can’t figure out where I’m going wrong.
I’d appreciate additional pairs of eyes. I’ll modify the code to work with Thanks in advance.debug
status to make a tag trace, and post that shortly.
Edit: I’ve switched the code to use debug
instead of live
so I can share a tag trace.
Code:
<txp:variable name="site_prod_status" value="<txp:php>echo $GLOBALS['production_status'];</txp:php>" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><txp:variable name="site_prod_status" /></title>
</head>
<body>
<p>1. var value [<txp:variable name="site_prod_status" />]</p>
<txp:if_variable name="site_prod_status" value="debug">
<p>2. var value if debugging [<txp:variable name="site_prod_status" />]</p>
<txp:else />
<p>3. var value is not debugging</p>
</txp:if_variable>
</body>
</html>
And the tag trace:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>debug</title>
</head>
<body>
<p>1. var value [debug]</p>
<p>3. var value is not debugging</p>
</body>
</html>
<!-- Runtime: 0.0041 -->
<!-- Query time: 0.001250 -->
<!-- Queries: 7 -->
<!-- Memory: 501Kb, <txp:php> -->
<!-- txp tag trace:
[SQL (0.00014996528625488): select name, data from txp_lang where lang='en-gb' AND ( event='public' OR event='common')]
[SQL (6.6041946411133E-5): select name, code, version from txp_plugin where status = 1 AND type IN (0,1,5) order by load_order]
[SQL (9.2983245849609E-5): select page, css from txp_section where name = 'default' limit 1]
[SQL (0.00022602081298828): select host from txp_log where ip='87.113.52.68' limit 1]
[SQL (0.00021100044250488): insert into txp_log set `time`=now(),page='/',ip='87.113.52.68',host='68.52.113.87.dyn.plus.net',refer='',status='200',method='GET']
[SQL (6.103515625E-5): select user_html from txp_page where name='default']
[Page: default]
<txp:variable name="site_prod_status" value="<txp:php>echo $GLOBALS['production_status'];</txp:php>" />
<txp:variable name="site_prod_status" />
<txp:variable name="site_prod_status" />
<txp:if_variable name="site_prod_status" value="debug">
[<txp:if_variable name="site_prod_status" value="debug">: false]
</txp:if_variable>
[ ~~~ secondpass ~~~ ]
<txp:php>
</txp:php>
<txp:php>
</txp:php>
-->
Note this line:
bc. [<txp:if_variable name=“site_prod_status” value=“debug”>: false]
The site is in debug
state at this point.
Last edited by gaekwad (2015-11-20 13:22:00)
Offline
Re: [solved] variable and if_variable strangeness
Use single quotes to delimit attributes that you want to have parsed:
<txp:variable name="site_prod_status" value='<txp:php>echo $GLOBALS["production_status"];</txp:php>' />
It was the two <txp:php></txp:php>
fragments below second pass that gave it away. Those were caused by the two places where you used <txp:variable>
, which returned the string <txp:php>echo $GLOBALS['production_status'];</txp:php>
in the first pass, which was converted to ‘debug’ on the second pass. But during the first pass, <txp:if_variable> compared that long string to ‘debug’ which of course is not the same.
Offline
Re: [solved] variable and if_variable strangeness
Maybe a problem with the very first line. When using a tag inside another, you’d better use single quotes: <txp:variable name="site_prod_status" value='<txp:php>...</txp:php>' />
. But as there are already single quotes in PHP code, it’s a bit tricky.
What happens when replacing it with
<txp:variable name="site_prod_status"><txp:php>echo $GLOBALS['production_status'];</txp:php></txp:variable>
Offline
Re: [solved] variable and if_variable strangeness
That’s it – I used the wrong quotes.
Thanks, Ruud – I am most grateful, sir.
Offline
Re: [solved] variable and if_variable strangeness
Aha! I can use variable
in a container, too!
Thank you, Claire: I greatly appreciate the reminder – I completely forgot about that.
Offline
Re: [solved] variable and if_variable strangeness
Pardon my ignorance, but if the variable value is set to debug
correctly, why does the if_variable
check evaluate to false?
I understand I used the wrong quotes in the first line, but the variable value appeared to be set to live
and debug
in each code block – so why did the check come back with the wrong result?
Offline
Re: [solved] variable and if_variable strangeness
During the first pass, the variable was set to <txp:php>echo $GLOBALS['production_status'];</txp:php>
.
The PHP code in that text wasn’t parsed until the second pass.
Offline
Re: [solved] variable and if_variable strangeness
ruud wrote #296717:
The PHP code in that text wasn’t parsed until the second pass.
Ah, that makes sense. Many thanks again, Ruud!
Offline
#9 2015-11-21 22:14:37
- gomedia
- Plugin Author
- Registered: 2008-06-01
- Posts: 1,373
Re: [solved] variable and if_variable strangeness
An option to streamline the code & perhaps make it a bit more “readable”:
<txp:adi_globals name="production_status" quiet="1" />
This will create a TXP variable called “production_status” containing the prod status value.
Offline