Calling parent template
This is an open discussion with 4 replies, filed under XSLT.
Search
I’m using hooks.
master.xsl:
<xsl:template match="data"> <body> do stuff 1 <xsl:call-template name="hookOne" /> other stuff 3 </body> </xsl:template> <xsl:template name="hookOne" />
page.xsl:
<xsl:template name="hookOne"> do stuff 2 </xsl:template>
You define the hook template in master.xsl. You can even put a default value there …
In page.xsl you simply override it with what you need but is NOT necessary to redeclare it if you don’t want to use it.
Output from page.xsl:
<body> do stuff 1 do stuff 2 do stuff 3 </body>
In my master.xsl i have something like this:
<xsl:template match="/"> <head> <link rel="stylesheet" type="text/css" media="all" href="{$workspace}/css/styles.css" /> <xsl:call-template name="links" /> </head> <body> ... </body> <script type="text/javascript" src="{$workspace}/scripts/jQuery/jquery-1.4.3.min.js" /> <xsl:call-template name="scripts" /> </xsl:template> <xsl:template name="links" /> <xsl:template name="scripts" />
On page5.xsl I need some extra CSS only. No extra .js I override only this template:
<xsl:template name="links"> <link rel="stylesheet" type="text/css" media="all" href="{$workspace}/css/more_styles.css" /> </xsl:template>
Output from page5.xsl includes the extra stylesheet.
I do something similar to vladG: inside master.xsl:
<xsl:template match="/"> ... <script src="{$workspace}/javascript/script1.js"></script> <xsl:call-template name="js-contextual"/> ... </xsl:template> <xsl:template name="js-contextual"> <script src="script2.js"></script> <script>var somevar = "no-param";</script> </xsl:template>
Then on somepage.xsl I define the template name again:
<xsl:template match="data"> ... </xsl:template> <xsl:template name="js-contextual"> <script src="script2.js"></script> <script>var somevar = "<xsl:value-of select="$param"/>"</script> </xsl:template>
edit: I think I’ve understood your question wrong - you want to call back up into the master right? Wouldn’t you call back using with-param?
Thanks for the answers.
Using your approach, you will lost what you initially defined in master.xsl
. Don’t know if you heard about twig, it does exactly what I’m after and maybe its documentation could clarify a bit.
What you are proposing is roughly the same approach I used so far, but there are cases where having a template that builds up incrementally, rather than totally override the previously defined one, could be quite useful.
Create an account or sign in to comment.
There’s something in xslt I have never figured out. Is it possible to call the parent of the current template?
Let me explain. Say I have two stylesheets
master.xsl
andpage.xsl
.master.xsl
looks like this:and this is
page.xsl
:Obviously, the first template in
master.xsl
is overridden by the second template inpage.xsl
. Question: is there a way to call the first template inside the second template, so that both get matched?This would be very useful (not sure if there’s an alternative approach). Thanks :)