Textpattern tips, tutorials and code snippets

Hide articles where the time is set to 00:00

Sometimes you’ll want to use Textpattern articles as events. A common approach is to use Textpattern’s “Posted Date/Time” field to store date/time information about the event. For example, user “totoff” at the TXP forums was using the <txp:posted /> tag to output the time of a vernissage (a private viewing that occurs before a gallery opening). He wanted to show only articles where the time wasn’t set to “00:00”, because this is the default setting and would indicate that there was no vernissage.

Our goal is to compare the posted date with the value “00:00” and see if there’s a match. If there is a match, there is no vernissage, so we’ll do nothing. If there is a mismatch, it means there is a vernissage, and we’ll output the vernissage data.

Store the information in a variable

First, let’s store the posted information in a variable, so that we can compare it later. One trick I learned about variables from Stef Dawson is that you can use them as a container tag, like so:

<txp:variable name="vernissage_time"><txp:posted format="%H:%M" /></txp:variable>

That creates a new variable called “vernissage_time” and sets its value to the posted date/time as “HH:MM” (Hours/Minutes).

Check the content of the variable

Next, we want to use <txp:if_variable> to check if vernissage_time is set to 00:00.

<txp:if_variable name="vernissage_time" value="00:00">

If that’s true, let’s do nothing. I like to put a comment in, like this:

<!-- no vernissage -->

Then let’s use the <txp:else /> statement for when we’ve got a time that’s different than “00:00” and thus a vernissage is being held:

<txp:else />
		<h2>Vernissage Time:</h2>
		<p><txp:variable name="vernissage_time" /></p>
	</txp:if_variable>

That’s done it! Here is the complete code:

<txp:variable name="vernissage_time"><txp:posted format="%H:%M" /></txp:variable>
<txp:if_variable name="vernissage_time" value="00:00">
	<!-- no vernissage -->
<txp:else />
	<h2>Vernissage Time:</h2>
	<p><txp:variable name="vernissage_time" /></p>
</txp:if_variable>

7 Comments Comment feed

  • Uli
  • 22 June 2012

Please note that only the variable code from the first variable example here (<txp:variable name="vernissage_time"><txp:posted format="%H:%M" /></txp:variable> ) would work, as the the second one from the “complete code” block would always contain some additional white space.

@uli – are you saying the complete code block will not work? I indented Marc’s code to make it more readable, so actually its not his doing…

  • Uli
  • 23 June 2012

Jonathan, the evaluation will never yield a true result as it never contains a mere, pure, 00:00, there’s always additional blank space in the variable.

  • totoff
  • 27 June 2012

uli, sorry to answer back, but in fact it does yield true. i did implement it on my site and it works like a charme: if posting time is set to 00:00, it omits the “vernissage” block.

  • Uli
  • 27 June 2012

totoff, I assume you’re using Marc Carsons code from his original forum post, don’t you? I’m sure it works.
But as soon as you put white space characters inside the variable declaration, the evaluation will stop outputting true.

@uli – how does it look now, I’ve edited the code but not tested the outcome.

  • Uli
  • 28 June 2012

Should work now, Jonathan.

Add a comment

Use Textile help to style your comments