Textpattern tips, tutorials and code snippets

Display content based on URL variable

With the release of Textpattern 4.07, we now have several new and powerful tags to play with. Here, we learn how to display a Flickr set based on a variable from the URL.

  1. First, download and install the excellent adi_gps plugin

The problem

The original problem was figuring out how to display Flickr sets based on an incoming URL request. What I needed:

  • If the URL is simply /gallery, display two pre-defined Flickr sets, using the vdh_flickr plugin
  • If the URL is something like /gallery/?set=12345678, then display only that Flickr set

Source the sets according to the URL

<txp:if_section name="gallery">
<txp:adi_gps name="set" quiet="1" />
<txp:if_variable name="set" value="">
<txp:vdh_flickr_thumbnails nsid="your_id" thumbnails_form="flickr_gallery" open="lightbox" set="your_set_id" />
<txp:vdh_flickr_thumbnails nsid="your_id" thumbnails_form="flickr_gallery" open="lightbox" set="your_set_id" />
<txp:else />
<txp:vdh_flickr nsid="your_id" thumbnails_form="flickr_gallery" open="lightbox" set='<txp:variable name="set" />' />
</txp:if_variable>
</txp:if_section>

In your page, place the above code, replacing the id’s where necessary. Here’s a rundown:

  1. The <txp:adi_gps name="set" quiet="1" /> tag extracts a variable called set from the URL, then creates a TXP variable of the same name and display its value
  2. The attribute quiet=“1” in the <txp:adi_gps name="set" quiet="1" /> tag restrains any output of the variable on screen
  3. The if_variable conditional checks if the variable with name set exists, and has a value
  4. If it does exist and the value is empty (coming from a URL /gallery), display two pre-defined Flickr sets
  5. If it does exist and the value is defined (coming from a URL /gallery/?set=12345678), display the Flickr set with the #id given in the variable

Create the form flickr_gallery

<h3><txp:vdh_flickr_thumbnails_title /></h3>
<p class="flickr_slideshow">
<txp:vdh_flickr_thumbnails_slideshow>&raquo; See the slideshow in another window</txp:vdh_flickr_thumbnails_slideshow>
</p>
<txp:vdh_flickr_thumbnails_if_description>
<p class="flickr_thumbnails_description"><txp:vdh_flickr_thumbnails_description /></p>
</txp:vdh_flickr_thumbnails_if_description>
<div class="flickrset">
<txp:vdh_flickr_thumbnails_list />
</div>

To see the result in action, you may visit this page, and try another link.

Thanks to Els on the forum for her help in figuring this out.

Use Textile help to style your comments