Textpattern tips, tutorials and code snippets

Display navigation if newer or older values exist

Textpattern offers an in-built method for paging result sets – page 1, page 2 and so on – via the txp:newer and txp:older tags.

A colleague was working on a site where he wanted the <div id="navigation"> to display only if there was a value for either <txp:older /> or <txp:newer />.

The first revision

This is the first code revision:

<txp:if_article_list>
<!-- entry navigation -->
<div id="navigation">
<txp:variable name="older" value='<txp:older />' />
<txp:variable name="newer" value='<txp:newer />' />
<txp:if_variable name="newer" value="">
<txp:else />
<div class="nav-links"><txp:newer>Next</txp:newer> &#8594;</div>
</txp:if_variable>
<txp:if_variable name="older" value="">
<txp:else />
<div class="nav-links">&#8592; <txp:older>Previous</txp:older></div>
</txp:if_variable>
<!-- entry entry navigation -->
</div>
</txp:if_article_list>  

Nice, but it does not work because #navigation is placed outside of the variable call. What we need to do is to place <txp:newer /> and <txp:older /> inside the variable.

The final code

<txp:if_article_list>
<txp:variable name="has_np" value='<txp:older /><txp:newer />' />

<txp:if_variable name="has_np" value="">
<txp:else />
<!-- entry navigation -->
<div id="navigation">
</txp:if_variable>

<txp:older>
<div class="nav-links">&#8594;
<a href="<txp:older />"><txp:text item="prev" /></a></div>
</txp:older>

<txp:newer>
<div class="nav-links"><a href="<txp:newer />"><txp:text item="next" /></a>
&#8594;</div>
</txp:newer>

<txp:if_variable name="has_np" value="">
<txp:else />
<!-- end entry navigation -->
</div>
</txp:if_variable>
</txp:if_article_list>

As you can see, we first check if either newer or older values exist and if so, place them in a variable: <txp:variable name="has_np" value='<txp:older /><txp:newer />' />. If the variable exists, <div id="navigation"> is displayed.

7 Comments Comment feed

Great!
Just this morning I tried to apply this tip!
Unfortunately without success.
Thanks Stef

Hi friends of TXP Tips,

I think I’ve spotted an error here. Nesting <txp:older /> (or <txp:newer />) inside a <txp:older></txp:older> (or <txp:newer></txp:newer>) will give invalid markup, as the container creates a link (<a>) around the contained code, which already has a link.

Also, if I’m not wrong, the “duplicated” if_variable for creating opening and closing div tags for #navigation could be converted in just one if_variable at top (well, just below the line were the variable has been created) that will output just all or nothing.

I’ll check on this Maniqui – in the meantime have any suggestions for changing the code?

  • Stuart
  • 22 May 2010

Does this work better?:-

<txp:if_article_list>
<txp:variable name="has_np" value='<txp:older /><txp:newer />' />

<txp:if_variable name="has_np" value="">
<txp:else />
<div id="navigation"> <!-- start entry navigation -->

<div class="nav-links"><txp:older><txp:text item="prev" /></txp:older></div>

<div class="nav-links"><txp:newer><txp:text item="next" /></txp:newer></div>

</div> <!-- end entry navigation -->
</txp:if_variable>
</txp:if_article_list>

though you can still end up with an empty “nav-links” div if one or other of the Txp tags is empty.

  • Stuart
  • 22 May 2010

So that was the short version. If my memory serves me right you can keep resetting the variable so maybe this would remove any empty divs:-

<txp:if_article_list>
<txp:variable name="has_np" value='<txp:older /><txp:newer />' />

<txp:if_variable name="has_np" value="">
<txp:else />
<div id="navigation"> <!-- start entry navigation -->

<txp:variable name="has_np" value='<txp:older />' />
<txp:if_variable name="has_np" value="">
<txp:else />
<div class="nav-links"><txp:older><txp:text item="prev" /></txp:older></div>
</txp:if_variable>

<txp:variable name="has_np" value='<txp:newer />' />
<txp:if_variable name="has_np" value="">
<txp:else />
<div class="nav-links"><txp:newer><txp:text item="next" /></txp:newer></div>
</txp:if_variable>

</div> <!-- end entry navigation -->
</txp:if_variable>
</txp:if_article_list>

and yes, I probably should have come up with that the first time but it is much easier to see when the code is there in front of you. ;)

Nice code Stuart! Stef came up with this code for me on a site I am working on. This is the final revision which works ok:

<txp:if_article_list>
<txp:variable name="has_np" value='<txp:older /><txp:newer />' />
	<txp:if_variable name="has_np" value="">
		<txp:else /><!-- entry navigation -->
		<div id="navigation">

	<txp:variable name="older" value='<txp:older />' />
	<txp:if_variable name="older" value="">
		<txp:else />
   		<div class="nav-links">&#8592; <txp:older><txp:text item="older" /></txp:older></div>
	</txp:if_variable>

	<txp:variable name="newer" value='<txp:newer />' />
	<txp:if_variable name="newer" value="">
		<txp:else />
   		<div class="nav-links"><txp:newer><txp:text item="newer" /></txp:newer> &#8594;</div>
	</txp:if_variable>

		</div><!-- end entry navigation -->
	</txp:if_variable>
</txp:if_article_list>

Both of those make sense, basically what Maniqui was suggesting. Maybe worth updating the original post after contacting Stef.

Add a comment

Use Textile help to style your comments