Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
#1 Yesterday 17:45:35
- abu
- New Member
- From: Switzerland
- Registered: 2025-04-03
- Posts: 4
Strange variable behavior
Something I simply don’t understand, maybe someone can help me out here.
Take this:
Value of variable is ***<txp:variable name="p" />***
<txp:if_variable name="p" value="0">
<p>Rendered if variable p == 0</p>
<txp:else />
<p>Rendered if variable p != 0</p>
</txp:if_variable>
Outputs that:
Value of variable is ***0***
Rendered if variable != 0
Why does this happen?
Offline
Re: Strange variable behavior
In other tag attributes, the value "0"
traditionally means “off”, so I guess the tag is interpreting 0
as meaning false
, so the condition is saying: “if variable p
doesn’t exist…”, but as the variable does exist, it’s showing you the other part of the if
conditional.
Can you use another value pair, e.g. "on"
and "off"
or "yes"
and "no"
? Or if you’re using a variable as a counter, start with "1"
…
I was wrong. Testing on the demo page with:
<txp:variable name="p">0</txp:variable>
Value of variable is ***<txp:variable name="p" />***
<txp:if_variable name="p" value="0">
<p>Rendered if variable p == 0</p>
<txp:else />
<p>Rendered if variable p != 0</p>
</txp:if_variable>
returns Rendered if variable p == 0.
TXP Builders – finely-crafted code, design and txp
Online
#3 Today 08:45:49
- abu
- New Member
- From: Switzerland
- Registered: 2025-04-03
- Posts: 4
Re: Strange variable behavior
Thank you for your prompt reply.
It was my bad, as mostly. My method for setting the variable using JavaScript didn’t work.
Offline
Pages: 1