Textpattern tips, tutorials and code snippets

Choosing a form dynamically

Here is a very short tip which may seem obvious, but had me stumped for a while today, so thought I would share. You may judge me accordingly ;-)

Desired result

Here is what I wanted to do: Display a portfolio cycle based on the category fed to the <txp:output_form form="something" /> tag. I have three categories setup:

  • Design
  • Illustration
  • Art

Using the txp:category tag I wanted to do something like this:

<txp:output_form form="<txp:category />_cycle" />

So the idea is for Textpattern to fill in my category name with something like this dynamically:

<txp:output_form form="design_cycle" />

And then I have a corresponding form conveniently named design_cycle ready for display.

It does not work!

Here is why – the new tag parser introduced in 4.07 changed the way tags are parsed. We don’t need to know the ins and outs – we just want it to work!

The correct usage

Here is the correct usage for my example above:

<txp:output_form form='<txp:category />_cycle' />

4 Comments Comment feed

While this is a good illustration of correct tags-within-tags syntax, I am curious if your three forms are similar enough that you could instead use a single form and <txp:yield />? That is:

<txp:output_form form=“porfolio_cycle”>
<txp:category />
</txp:output_form>

To be honest have not got around to txp:yield yet and was hoping too soon. The 3 forms are quite different so I am not sure if I can use yield here.

Probably not a good case for yield, then. I finally came up with a reasonable use for it, and have just revised my article on static CSS accordingly — see the end of the article.

Nice posts there. Thank’s for the interesting information.

Add a comment

Use Textile help to style your comments