Outputting textareas containing <pre> in Symphony
When I was creating my custom templates for Symphony, I reused a lot of code from the default template. This helped me learn how Symphony functioned and how to write XSLT. Because the default template used wildcard selectors when outputting data in textareas, my template did too. It worked fine until I had a post with code blocks in it and then I had a problem.
The default template way
<xsl:copy-of select="body/*"/>
The default template uses a wildcard selector which will select all element children of the ‘body’ node and copy every node individually. The resulting XML is nicely indented — even inside <pre> elements.
This is not good if you have any articles with code blocks in them. That extra whitespace that the indenting uses between the <pre> and <code> tags show up in your code block in the browser messing up your code formatting.
Solution one
One way that allows the widcard method to work with <pre> is to use the <output> element in the stylesheet and turn off indenting.
<xsl:output indent="no"/>
This solves the problem, but all of your resulting XML will not be indented. This may not be a big deal since Symphony’s ?debug screen will indent the code for you making debugging easier.
Solution two (the option I like)
I like clean code and I think output indenting is a good thing so I needed a different option. Another way to select child nodes is to use the node() function.
<xsl:copy-of select="body/node()"/>
This will select all element children of the ‘body’ node and copy them all at the same time. The resulting output will not be indented and no extra whitespace will be added to your code blocks.
It’s not a perfect solution because all the code inside the textarea won’t be indented when transformed into XHTML, but the rest of the page will and that’s good enough for me.
Comments // What has been said about this entry
Hey I read your article and I have been having the same issue on my blog for months. I always thought it was some crazy browser quirk, but never the xml indentation being translated onto the page!
Great solution, and some good detective work :]