Feedback: 12 comments
As of version 4.0.7, Textpattern has a few new strings to its bow. One of these is the concept of economical storage and retrieval via
<txp:variable /> but for those without a coding mindset it can appear tricky at first.
We caught up with this new tag We caught up with this new tag… and asked it to keep things simple for onceas it was preparing to bamboozle some more innocent people and asked it to keep things simple for once.
TXPQ What is life all about?
txp:variable Being flexible, inventive and useful.
TXPQ Tell us about your childhood.
txp:variable Since I was born my parents brought me up to make the most of what I’ve got. I was a little sensitive about my weight — just 10 lines of code — but they told me bedtime stories about how I’d change the way people use Textpattern. Above all, they taught me that with my in-built flexibility, I could provide site designers with a little programming ability without the need for PHP.
TXPQ But programmers are strange creatures; should we be worried that TXP is turning into a geeks-only tool instead of for designers?
txp:variable lol, not at all. I can appeal to designers as well as codersIf you can’t see a use for me right away, that’s fine. I’m there as a convenience if you ever need me. And I’m sure I can appeal to designers as well as coders if you give me a few minutes. I don’t bite.
TXPQ OK, ummm, so convince us. Ease us into your mindset.
Save it for Later
txp:variable I’m all about storage and retrieval and keeping pages fairly lean by avoiding duplication. One of the simplest things you can do is store a single value you use again and again across your pages.
For example, if you use the same URL a few times, instead of hard-coding it into all your forms or pages, you can define it once and refer to it; a bit like adding a URL to the bottom of an article and referring to it many times with Textile.
Doing things this way means that if you ever need to change the URL in future you can do it in one place and it gets changed everywhere. Perhaps in a form called
<txp:variable name="megablog" value="<a href='http://my.megablog.com/is/here'>MegaBlog</a>" />
<txp:variable name="coderblog" value="<a href='http://blogs.com/~foo/coder'>Coding blog</a>" />
If you use
<txp:output_form form="constants" /> at the top of your page template, the two variables called
coderblog then become available to be used freely wherever you like; even in articles!
To retrieve the value at any time you would write:
Don’t forget to visit my
<txp:variable name="megablog" />to learn about how cool I am.
You could even assign the
<txp:variable /> as a QuickTag if you use it often enough.
TXPQ So once a value is set, is that it?
txp:variable Not at all. You can set and reset any value as often as you like during the course of the pageYou can set and reset any value as often as you like during the course of the page. Let’s run through an example, step by step:
<txp:variable name="where_are_we" value="A page" />
Initially the value of the variable named
"where_are_we" is set to
"A page". If nothing is done to alter that variable from now on, it will stay this way. But:
<!-- This conditional section is only entered if the visitor is using the search feature -->
<txp:variable name="where_are_we" value="We're in a search" />
If a search is in progress, the variable named
"where_are_we" now has its value changed to
"We're in a search". If, however, a search is not in progress, at this point the variable’s value would still be
<!-- This conditional section is only entered if the visitor is browsing by category -->
<txp:variable name="where_are_we" value="I lied, it's a category list" />
means that — similarly to above —
"where_are_we" is only altered if the visitor is browsing a category. If they were not browsing a category, the value would be left as it was (which is either
"We're in a search" if the visitor was searching for something, or
"A page" if they weren’t).
So if the above I have set and subsequently changed the value depending on what the visitor is doingthree chunks of code are put together I have set and subsequently changed the value depending on what the visitor is doing. At some point later you can print out my latest value with:
<txp:variable name="where_are_we" />
The result of that statement would display one of the following:
|if visitor is||result would be|
|Browsing a category||
|Searching the site||
|Doing anything else||
TXPQ Seems simple enough. What’s the next step?
Parse the Sauce
txp:variable With the new tag parser in TXP 4.0.7 I can be used inside other tags to pass values into them:
<txp:variable name="category_type" value="image" />
<txp:variable name="category_type" value="article" />
<txp:category_list type='<txp:variable name="category_type" />' />
That’s very useful, since repeating the
<txp:category_list /> tag and, potentially, all its other attributes inside both branches of the
<txp:if_section /> clutters the form and is a pain to update. I can be used to simplify page structure and help make sure you only have to refer to stuff onceSo I can be used to simplify page structure and help make sure you only have to refer to stuff once.
One thing to note: the single quotes around me when inserted inside another tag tell the parser to go ahead and parse me, putting the result inside the
type attribute. If the tag had been written like this instead:
<txp:category_list type="<txp:variable name="category_type" />" />
it may have worked in this simple example but other times it might not. The rule of thumb is: if you want to put a tag as an attribute, surround the contents of the attribute in single quotes.
TXPQ It’s starting to make sense at last! You could be used the other way round, I assume? Other tags can be put inside you?
txp:variable Of course. But on my own that may not be incredibly helpful since all tags can take other tags directly now. Perhaps this is a good time to introduce my brother.
TXPQ A sibling?
txp:variable Yep. Meet
<txp:if_variable />. He can look at anything I have stored and test it, so action can be taken based on what value is stored.
TXPQ Like a kind of generic
<txp:if /> ?
My brother is check for the existence of a variable — that is, a variable that has been defined earlier in the pagevery quiet and unassuming in daily life but rather powerful when poked; a bit like Wolverine. Firstly, he can check for the existence of a variable — that is, a variable that has been defined earlier in the page and may or may not have a value:
<txp:variable name="test_me" value="" />
<txp:variable name="test_me_too" value="Something in here" />
<txp:if_variable name="test_me"> <p>Yes, I exist, even though I don't have a value</p> </txp:if_variable>
<txp:if_variable name="test_me_too"> <p>I exist as well! And I have a value</p> </txp:if_variable>
In both cases above, the value inside the
<txp:if_variable /> statement will be displayed.
value="" can test for the existence of an empty variable, which is quite useful when the value you assign to the variable is the result of another tag. That means you could assign the output of
<txp:keywords /> to a variable and then take different action depending if there were any keywords returned or not. So essentially you can create a brand new tag –
TXPQ So let me get this right. Can I do this using your example above?
<txp:variable name="has_keywords" value='<txp:keywords />' />
<txp:if_variable name="has_keywords" value="">
<p>Article keywords: <txp:keywords /></p>
<p>No keywords listed for this article</p>
[ EDITED to add
value="" , thanks maniqui / Els / wet ]
I just I just created a new tag? Wow!created a new tag? Wow!
txp:variable More or less, yes. Cool huh?
All Things Equal
TXPQ So he’s your brother but he certainly ain’t heavy — just 15 lines of source code! You said he can help in two ways…
txp:variable That’s right. The other way is to test if a particular variable matches a given value. For example, the
<txp:if_author /> tag only works within search criteria, but what if you wanted to take some action on every page based on who authored an article?
You could do this somewhere near the top of your page template:
<txp:variable name="this_author" value='<txp:author />' />
Which would assign the current author to the variable labelled
this_author. Then, sometime later in your page template you could put:
<txp:if_variable name="this_author" value="peter">
<txp:css n="a_different_style" />
apply alternating colours without plugins, thus keeping your pages nimble and your plugin count downSo any articles written by Peter use a different stylesheet. You could apply this to comments to more easily highlight author comments or apply alternating colours without plugins, thus keeping your pages nimble and your plugin count down.
TXPQ Hey that’s neat. So what else can you do?
And There’s More
txp:variable Plenty. There are some esoteric uses over on the forums.
We can really be of value for applying little graphical or typographical niceties such as those examples, or for radically simplifying the page structure to keep your pages trim, lean and light in the true spirit of Textpattern. And of course everything in between.
TXPQ Excellent! Thank you for opening our eyes to the possibilities. We wish you much variable goodness in years to come.
<txp:variable name="sayonara" value="Thanks. You're welcome!" />
<txp:if_variable name="interview" value="over">
<txp:variable name="sayonara" />
If you have found any cool uses for me and my brother, post a comment about it and let us know.
Author: Stef Dawson
Commenting has expired for this article.
Testing if there’s a problem with comments. If this is accepted OK, then it must be something you are typing in. Here’s a Textile link and here’s some textile syntax. If this is working OK, please let me know what is going wrong for you.
maniqui wrote (on the forum)(I’ve swopped some brackets for less than and greater than which for some reason it doesn’t like):
Hilarious! Great article, keep coming more of this tags interviews…
Regarding the examples, I think I’ve spotted one or two errors in the article examples.
(I’m posting here at forums for two reasons: Preview mechanism is failing on TXPQ, and also, so we can discuss the following)
In the keywords example,
… the conditionals are testing for the existance of the variable, so it will always be true.
So, shouldn’t it be the following?
And, BTW, Jon-Michael has been testing it on his SVN installation, and he told me at IRC that the
value=""isn’t working, could Bloke confirm this?
Regarding the author example (
(txp:if_variable name="this_author" value="peter")...), wouldn’t it be the same to use @(txp:if_article_author).
I understand the autor example was just that, an example, thought the example may be a little confusing for newcomers (because the tag already exists, and the example is just trying to emulate it).
Thanks again for this kind of articles, zero and Bloke.
Can’t remember if you have already interviewed
Be careful, that tag could suffer of schizophrenia… :D
Damn, if the less than/greater than signs would have worked, I’d have been first to post this… ;)
But anyway, lovely article!
Oh, my Queen, thou deserve all the credits, for being my muse.
In the keywords example, it’s an if_variable so if there is nothing to report (not even a blank), there’s no output. Therefore, it’s only if there are keywords that something can be output, so that’s why the txp:else is put later not first).
But JM finds value=”“ does not work, so perhaps your example is also correct and will work, provided value=”“ can be made to work in the way you think it should. But perhaps it is meant to work the way it works now because this is not just any tag from what I understand.
(txp:variable name=“beware_my_logic” value=“1p” /)
Rick Silletti #
I plugged Robert`s zebra list tutorial, using txp:variable and txp:if_variable, into a site a friend and I are working on when time permits. I`m using it to get use to 4.0.7 and the going is simple and slow, but it might give some an idea as to the potential of the most recent changes; some rather intense possibilities. The site is at Blues – just poke around “Links by Category” and do be kind, we are as yet bereft of much in the way of content – life is busy!
txp:if_variable name="foo" value=""won’t work with the current state of the tag. This is an oversight which will be corrected ASAP.
Stef (Author) #
Bah! Thanks for the corrections guys ‘n’ gals. Swear I tested ‘em but obviously not hard enough. Sorry for any mixups, I’ll fix that example to add
value=""so that when the change comes online, the article will be correct.
And yes maniqui, the last example will probably do the same as if_article_author. Never even noticed that tag, thanks for the heads up. You’ll have to just take the sentiment of the example and apply it elsewhere :-) Should I change it for some other example or leave it?
Markus Merz #
Very, very nice! That’s how I love documentation as an intellectual side dish with my chicken wings.
I think the pesky example is still not correct, no? Am I a tard or is the switch back to front?