Textpattern tips, tutorials and code snippets

Enter HTML into an article title

There are many instances where an author might want or need to add a bit of HTML to an article title. For example, to hard code a break:

<h2>My Story <br />Is Better Than Yours!</h2>

or if a title of the book is contained within:

<h2>I reviewed <cite>A Tale of Two Cities</cite></h2>

Since you cannot use Textile in the title field, you might be tempted to simply insert your HTML, but that would result in this showing up on the page:

I reviewed <cite>A Tale of Two Cities</cite>

and this in the underlying html:

I reviewed &#60;cite&#62;A Tale of Two Cities&#60;/cite&#62;

The Solution

The solution is to:

  1. Use a placeholder for the html elements you want
  2. Use a bit of php to output the title

For our title example, you could enter the title as:

I Reviewed {A Tale of Two Cities}

and then use this in your article form to output the title:

<h2><txp:permlink><txp:php>
       global $thisarticle;
       assert_article();
       $my_title = str_replace('{', '<cite>', $thisarticle['title']);
       echo str_replace('}','</cite>',$my_title);
   </txp:php></txp:permlink></h2>

Using a placeholder is important so that the titles in your RSS feed or output into the html <title> element don’t contain html elements.

Thanks to jsoo on this thread for the tip!

7 Comments Comment feed

You could even use this workaround to use Textile in article titles. Just bear in mind the caveats pointed out in the Txp forum thread linked above — it’s up to you to make sure the raw Textile looks OK in RSS feeds, etc. Obviously the Txp team have decided that Textile in article titles is Not, On Balance, A Good Thing, so be advised.

You could do this by replacing <txp:title /> with:

<txp:php>
global $thisarticle;
assert_article();
include txpath.’/lib/classTextile.php’;
$txt = new Textile;
echo $txt->TextileThis($thisarticle[‘title’]);
</txp:php>

Naturally you’d want to put this in a form if you want to use it more than once.

Ack, what am I (not) thinking — titles DO take Textile — please ignore or delete the above!

Ack again, no I was right the first time. Sheesh, just ignore me now…

  • Keith Reeder
  • 29 June 2009

upm_textile will help here, Nora and J…

Care to post an example Keith?

@Keith: good point!

@Jonathan: This ought to work:

<txp:upm_textile><txp:title /></txp:upm_textile>

@jsoo – that looks a lot easier!

Add a comment

Use Textile help to style your comments