Skip to content

Applying Filters to Titles

djacobs edited this page Oct 3, 2011 · 6 revisions

Actually, this can be used to apply text filters to anything. In my Entry Summary and Entry Detail templates, I originally had this:

 <h1><a href="<$MTEntryPermalink$>"><mt:entrytitle></a></h1>

Which works fine, but what I really wanted was to apply Smartypants to get proper punctuation. So I came up with this:

 <mt:setvarblock name="markdown_title"># <a href="<$MTEntryPermalink$>"><mt:entrytitle></a></mt:setvarblock>
 <mt:getvar name="markdown_title" filters="markdown_with_smartypants">

Which does exactly what I want. It also means I could do things like add asterisks around words in a title to make them bold or italic.

Note, however, that if the data you're setting in <mt:setvarblock> is multiple lines, you'll need convert_breaks="0" on the tag where the original data comes from. Otherwise, it will have <p></p> tags added, and Markdown will skip their contents. For example: &lt;mt:setvarblock name=&quot;comment&quot;&gt;&lt;$mt:CommentBody$&gt;&lt;/mt:setvarblock&gt; <$mt:Var name="comment" filters="markdown_with_smartypants"$> ...will ignore the Markdown, showing the Markdown formatting characters instead of HTML markup. While: &lt;mt:setvarblock name=&quot;comment&quot;&gt;&lt;$mt:CommentBody convert_breaks=&quot;0&quot;$&gt;&lt;/mt:setvarblock&gt; <$mt:Var name="comment" filters="markdown_with_smartypants"$> ...will work as expected, with Markdown adding paragraph breaks in addition to formatting markup. Likewise, removing paragraph tags from what's intended to be a single line of text can be done with this: &lt;mt:setvarblock name=&quot;comment&quot;&gt;&lt;$mt:EntryTitle convert_breaks=&quot;0&quot;$&gt;&lt;/mt:setvarblock&gt; <h1><$mt:Var name="comment" filters="markdown_with_smartypants" regex_replace="/<\/?p>/g",""$></h1> That will produce a Markdown-formatted single line, without surrounding &lt;/p&gt;</mt:setvarblock> tags.

Category: Template Tag Recipes

Clone this wiki locally