Best practices re: snippets/partials and loops vs. templates
This is an open discussion with 6 replies, filed under XSLT.
Search
Templates/For-each: I’d go for templates. They’re leaner, you can do recursion with a single finger snap and use the full potential of overloading templates.
The size of your XML isn’t nearly large enough for the performance-difference to have any impact.
What exactly do you mean by “global HTML”? <html />
, <body />
, your menu etc.? Have a look at the template structure in the default workspace (page.xsl
imports master.xsl
), it’s very similar to what I end up doing all the time.
@Phogue thanks.
I figured templates are probably the more elegant solution. Thanks for reinforcing that notion ;)
As for ‘snippets’: I totally get the master.xsl
vs. page.xsl
importing. What I was looking for was something like <xsl:include>
for ‘static’ snippets: this might be the site’s footer or contact info, or anything (not necessarily xsl templates but static HTML).
<xsl:include>
seems not appropriate since it is a top-level element, I cannot use it in another xsl template. This is why I am currently using <xsl:import>
in combination with <xsl:call-template>
.
You can wrap that static HTML in a <xsl:template name="footer" />
and call it using <xsl:call-template name="footer" />
.
Right. I would need to <xsl:import>
it before I can call
it, correct?
Correct.
Bear in mind a key difference between xsl:include
and xsl:import
is that the rules in import
ed stylesheets will be overridden by matching rules of equal priority in the import
ing stylesheet.
include
ed stylesheets, on the other hand, are basically treated as though their rules were part of the include
ing stylesheet, which can lead to conflicts between matching rules of equal priority.
Create an account or sign in to comment.
As I am learning XSLT/Symphony I’ve noticed that there’s almost always various ways of doing the same thing.
That’s wonderful, but obviously I’d like to learn the best/most efficient ways ;-)
Some documentation on (general) XSL best practices would be of help.
I’ve got two particular questions:
xsl:for-each
loop or use axsl:template
. A template seems more elegant, allowing more flexibility. But are there any other pointers as to what method to choose? (e.g. Performance?)At the moment I use a
xsl:import
with a ‘utility’ that merely contains some HTML (no xslt functionality. Are there other, better, ways of including snippets of recurring HTML?