Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-06-23 01:30:15

skewray
Member
From: Sunny Southern California
Registered: 2013-04-25
Posts: 225
Website Mastodon

Checking for empty variables

I have the code:

<txp:variable name="meta_error" value="<txp:error_message />" />
<txp:if_variable name="meta_error">
    <meta name="description2" content="<txp:error_message />">
</txp:if_variable>
<txp:if_variable name="meta_error" value="">
    <meta name="description3" content="empty">
<txp:else />
    <meta name="description3" content="none">
</txp:if_variable>

For a page that is not an error page, I get the output:

    <meta name="description2" content="">
    <meta name="description3" content="none">

So the first test says that the variable is not empty and then prints something empty. The second test also tests for empty and says not empty. What is the story here?

Offline

#2 2013-06-23 03:09:14

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Checking for empty variables

skewray wrote:

first test says that the variable is not empty

Without a value specified, the tag checks whether the variable is set, not whether it’s empty.

Offline

#3 2013-06-23 05:37:18

skewray
Member
From: Sunny Southern California
Registered: 2013-04-25
Posts: 225
Website Mastodon

Re: Checking for empty variables

As far as the first test, the manual states “the tag returns true if the named variable contains any value at all (i.e. it is not empty, or is defined).” This disagrees with your statement, implying that the manual is wrong? The manual is certainly confusing and should written more clearly.

What about the second test? Comparing “” to “” gives false.

Offline

#4 2013-06-23 09:18:11

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Checking for empty variables

skewray wrote:

implying that the manual is wrong?

You are right, the manual was wrong. It is now corrected. Thanks for finding this!

Offline

#5 2013-06-23 10:07:48

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,172
Website GitHub Mastodon Twitter

Re: Checking for empty variables

I also see an error in the code on the top. It should be

<txp:variable name="meta_error" value='<txp:error_message />' />, Note the single quotes.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#6 2013-06-23 15:18:48

skewray
Member
From: Sunny Southern California
Registered: 2013-04-25
Posts: 225
Website Mastodon

Re: Checking for empty variables

The manual is now super-clear.

I don’t get the difference between single and double quotes. Online examples seem to switch randomly between the two types.

Offline

#7 2013-06-23 15:26:01

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Checking for empty variables

Double quotes should be used normally. Single ones only in case you’re using TXP tags as values of TXP tag attributes, like in colak’s corrected code. Parsing single quotes is a tiny bit slower as the parser has to test whether the enclosed code is a TXP tag or a string (IIRC).

In case you see randomly used quotes in the manual: Please throw rocks!

(Edit: typos)

Last edited by uli (2013-06-23 15:27:05)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#8 2013-06-23 17:35:25

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Checking for empty variables

skewray wrote:

I don’t get the difference between single and double quotes.

This will help: Parsing Tag Attributes

Offline

Board footer

Powered by FluxBB