Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-09-08 01:08:02

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

Don't indent your code when testing for an empty variable

Is that true?

I had the following code in a form, nicely indented:

<txp:variable name="aktuelles-tester">
  <txp:article_custom section="aktuelles">
    <txp:title />
  </txp:article_custom>
</txp:variable>

<txp:if_variable name="aktuelles-tester" value="">,aktuelles<txp:else /></txp:if_variable>

Which always returned “false”. Only when I removed any white space from the inner part of the variable the code worked. Logical, isn’t it? Or ist it just because I worked too much this night?

(But we’ve tons of if_variable code here in the forum. Did noone ever stumble upon it?)

Please enlighten me!

Last edited by uli (2011-09-08 01:16:19)


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

Offline

#2 2011-09-08 06:32:29

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

Re: Don't indent your code when testing for an empty variable

uli wrote:

Is that true?

Yes. The contents of a variable is not trimmed from a white-space, or modified in any other manner. If you want to test the contents against empty, then you will need to make sure that there is no extra white-space, i.e. put everything to single line. For example, with your above example, the variable will have white-space as a content even when there are no articles.

If you remove white-space around article_custom and you are good to go:

<txp:variable name="aktuelles-tester"><txp:article_custom section="aktuelles" limit="1">
	<txp:title />
</txp:article_custom></txp:variable>

(But we’ve tons of if_variable code here in the forum. Did noone ever stumble upon it?)

Honestly, I haven’t really seen any incorrect variable code here on the forum, not saying that there isn’t. This is also the reason why I write my tips/examples to unreadable single line. It’s not because i want to, it’s because have to. Plus making the variable size smaller, decreases memory used by couple bytes.

Offline

#3 2011-09-08 09:09:16

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

Re: Don't indent your code when testing for an empty variable

It would be nice to have a trim attribute in there but in the meantime if you have really complex stuff in your variable you can also use one of Jukka’s other plugins:

<txp:variable name="aktuelles-tester"><txp:rah_function call="trim">

  <txp:article_custom section="aktuelles" limit="1">
    <txp:title />
  </txp:article_custom>

</txp:rah_function></txp:variable>

(a trick learnt from Jukka himself)


TXP Builders – finely-crafted code, design and txp

Offline

#4 2011-09-08 11:49:10

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

Re: Don't indent your code when testing for an empty variable

Jukka, thanks for the clarification. That little bit of indentation inside the article tag should suffice most of the times. And if I need a whitespace spree I can always use your function plugin.

jakob, thanks for offering those continued sprees. And yes, a trim attribute would be nice. We’d still have to remember it (probably ;) but having it could serve an improved readability.

BTW Wouldn’t chh_if_data do the same job? And wasn’t there a way to replace chh_if_data with a var/if_var combo? Els, do you hear me? ;) <brain_loupe>To me these constructs always look like nails you’ve forgotten to countersink. This one would be a nail inside a nailhead. Cool!</brain_loupe>


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

Offline

#5 2011-09-08 20:53:29

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

Re: Don't indent your code when testing for an empty variable

uli wrote:

(But we’ve tons of if_variable code here in the forum. Did noone ever stumble upon it?)

It’s not a problem with if_variable, only with the txp:variable tag.

And wasn’t there a way to replace chh_if_data with a var/if_var combo? Els, do you hear me? ;)

This forum needs a Facebook/Diaspora/Google+-like mention feature ;) But why this question, because that is what you just did…

<txp:chh_if_data>
   <div><txp:tag /></div>
</txp:chh_if_data>

becomes

<txp:variable name="if_data" value='<txp:tag />' />
<txp:if_variable name="if_data" value="">
<txp:else />
   <div><txp:tag /></div>
</txp:if_variable>

A little bit longer, but much more flexible.

Offline

#6 2011-09-08 22:17:33

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

Re: Don't indent your code when testing for an empty variable

Els wrote:

But why this question

Sorry if I made you feel like I urged you to post any code, wasn’t my intention. No, just that my mind began throwing sparks while thinking of removing white space off an otherwise empty variable with a second variable/if_variable construct.

This forum needs a Facebook/Diaspora/Google+-like mention feature ;)

Naaa, no, never! It lacks the human, the really social, component: to be sure that another person will read certain topics :)


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

Offline

#7 2011-09-08 22:50:30

hcgtv
Plugin Author
From: Key Largo, Florida
Registered: 2005-11-29
Posts: 2,722
Website

Re: Don't indent your code when testing for an empty variable

uli wrote:

Naaa, no, never! It lacks the human, the really social, component: to be sure that another person will read certain topics :)

+1 ;)

On the subject of indenting code, I don’t like to indent template code on my sites. Where indenting code in a text editor will facilitate understanding of said code, when you copy/paste it into the backend, especially with size of the default textareas, it becomes rather confusing to maintain from that point on.

I know some prefer to use cnk_versioning or another method to code and maintain their sites, I prefer to work on the backend. And yet one of the gripes with Textpattern is that it keeps all template code in the database, which I don’t find to be a minus but rather a plus. To each his own I guess.

Offline

#8 2011-09-08 23:41:57

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

Re: Don't indent your code when testing for an empty variable

uli wrote:

Naaa, no, never!

+1 :) And to attract my attention writing ‘variable’ in the topic title will do… ;)

Offline

#9 2011-09-09 12:59:42

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

Re: Don't indent your code when testing for an empty variable

Els wrote:

And to attract my attention writing ‘variable’ in the topic title will do… ;)

I lay claim to hold copyright for the following topic titles: “Variable me”, “Need variable”, “Variable!!!!” in any of its mean derivative forms. Hope that helps stopping a plague of annoying titles popping up.


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

Offline

#10 2016-07-25 12:17:29

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Don't indent your code when testing for an empty variable


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

Board footer

Powered by FluxBB