Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Yesterday 17:45:35

abu
New Member
From: Switzerland
Registered: 2025-04-03
Posts: 4
GitHub GitLab Mastodon

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

#2 Yesterday 20:12:44

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,881
Website GitHub

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

Offline

#3 Today 08:45:49

abu
New Member
From: Switzerland
Registered: 2025-04-03
Posts: 4
GitHub GitLab Mastodon

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

Board footer

Powered by FluxBB