Journal
Pseudo TXP If_Not
15 June 2006 › 18 comments
There are a myriad of plugins out there for Textpattern, and I get quite a few emails asking if I know of ones that add negative conditionals. Being the perfectionist that I am though, I tend to shy away from plugins unless there is simply no other solution to the problem. This is for two reasons: First, I don’t like having to keep track of all of the plugins I am using, and the URL‘s for each one. Secondly, sometimes developers cease work on their projects, and Textpattern upgrades cause these older plugins to be broken.
I figured this out awhile ago on my own, and then kicked myself when I saw it already covered in the Textpattern FAQ. Yet, I think it bears repeating here. I give to you the super-duper no brainer pseudo method for creating negative conditionals. Let’s say, for example you want to do something only if you are not on a certain section, named “example.” That would look like this…
<txp:if_section name="example"><txp:else />
Do something here.
</txp:if_section>
See what’s going on there? Rather than place something between the opening txp:if_section and the txp:else, there is nothing. So, if it is that section, no content will be displayed or action taken. However, if it is not the section named “example,” then your specified content will be displayed. Now, in some programming languages, this would throw an error. In Textpattern though, you’re golden. This works with a variety of TXP tags, such as categories. Hopefully this will help, and save you time searching for specific plugins.
Discussion + Dissension
Comments closed after 2 weeks.



#1 matt
It’s little helps like these that help me keep my sanity and save me hours of work. By the way, I dig the blue and brown. Did you tweak the blue in the sky too?
#2 Nathan Smith
Matt: Thanks. Nope, I just sampled from the header to get colors for the sides. In the end, I went with easier to remember variants: #456 on the left, #654 on the right. It just seemed poetic to have them be numerical opposites, and the way hex colors work, ended up being somewhat complimentary.
#3 Jared Christensen
You should write a book.
#4 Lowell
do you know if you can do if_else conditionals in txp? i’ve tried but it never works. Very enlightening article by the way, thanks.
#5 Nathan Smith
Lowell: Yes, that’s actually what I described in this article. If you were to place content before the
txp:elsetag, that would be your if statement. Everything after that becomes your else statement. If you’re talking about PHP if/else logic, then you can use thetxp:phptags to enclose it. This lets one “punch a hole” so to speak, in Textpattern to get to the underlying PHP. I hope that helps you out a bit. You can read more about that in this wiki post.#6 Lowell
I meant like
if blah=blah
something here
if else blah=blaw
something here
if else blah=blab
something here
else
something here
something like that.
#7 Nathan Smith
Lowell: Yeah, that’s a bit too complex for Textpattern’s templating syntax. Think of it as the same level of complexity as Smarty (roughly). If you knew the specific variable names that you wanted to check against, you could always use raw PHP to do that though. Or, you could write a plugin!
#8 Robert Wetzlmayr
Somehow related and not the same: What, if you wanted to exclude a certain portion of an article, a page template or a form but wanted to keep your carefully crafted tag sequence contained in that portion of code for further reference or later use?
Textpattern obeys no HTML comments but parses all tags so you need another method of keeping you markup while preventing its execution.
I have adopted a habit to enclose such snippets with a conditional which is never true. For instance:
< txp:if_section name="dummy-does-not-exist">[...] valuable string of knowledge [...]< /txp:if_section>I know that there is a plugin somewhere which does just the same, but I prefer built-in over plugged-in functionality, too.
#9 Nathan Smith
Robert: I’m not sure I understand what you mean. As far as I can tell, the traditional exclamation point, double-dashed HTML comments seem to work just fine. I have never had a problem using comments within Textpattern. Again, maybe I’m not grasping what you’re trying to say.
#10 Robert Wetzlmayr
Try this:
< !-- < txp:article /> -->txp:article will be executed though it is included inside a comment. Imagine you wanted to exclude a certain part of a page’s template from getting rendered to HTML without erasing it completely: Enclosing that part with an always-false conditional tag is a handy method.
#11 Nathan Smith
Ah, I getcha. Yep, that method seems like a good one in those cases.
#12 Lowell
Nathan, I figured out how to do what I wanted to do and I thought I would share it here.
< txp:if_something name="blah, blah, blah">
put what you want here
< /txp:if_something>
< txp:if_something name="blah, blah, blah">
put what you want here too
< /txp:if_something>
and you can just continue adding those on to each other until you have enough if_somethings
#13 Nathan Smith
Lowell: Ah, I didn’t realize that’s what you were shooting for. I thought you really wanted else statements. The example you gave is what I do extensively whenever making a site with Textpattern. For a slew of examples, check out this tutorial article: Godbit Textpattern Code.
#14 Markus Merz
Thanks! These little tipps … this article just helped me. Now I don’t have to remember to add a section to the name= list if I add another one :-) In my case I only wanted one specific header item not to show on the homepage but on all other pages.
And I believe it’s also a little performance booster because TXP doesn’t have to check all conditions from the name= list if only one section is excluded.
#15 WD Milner
I “discovered” this technique previously myself, only to learn later it was in the TxP FAQ – mea culpa :)
As for comments placement, a safer method is to use the opening and closing angle brackets of the tag as part of the comment structure.
#16 Nathan Smith
Wilson: I would have to agree. I think it’s always best to keep a minimal workload for the server. With tons of conditionals, even those that evaluate to false, you slow down the page load, because it’s running PHP for each and every if / then statement.
#17 Brian Zollinhofer
I have a question that isn’t about this, but still with TXP. First, is there a good place that has a lot of tutorials about how to implement all of the things you can do with TXP? Second, the comments are listed with numbers (this one is #17). What is the purpose of the numbers having a link attached to it? When I clicked on the link, it just took me to that comment. I’m just not sure of the purpose (especially being so new to TXP). Third, I’m currently trying to get my comments to look better. I found a tutorial on Wilshire|One which seemed to be really good, but is a year old and may not work on the newer version of TXP (at least I couldn’t get it to work – but again, I’m a TXP n00b). Thanks for any thoughts/comments/help.
#18 Nathan Smith
Brian: The purpose of having each comment with a link is so that people can link to a particular comment within a page. For instance, if I wanted to refer people to your comment, I could copy that link into an email, etc. Without that, we would only be able to link to the top of each page, which isn’t very helpful with a long list of comments. As far as tutorials, the one on Wilshire One should be just fine, because it is still quite applicable.