Search

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 and page.xsl.

master.xsl looks like this:

<xsl:template match="data" mode="js">
    &lt;script type="text/javascript" src="something">&lt;/script>
</xsl:template>

and this is page.xsl:

<xsl:template match="data" mode="js">
    &lt;script type="text/javascript" src="other scripts">&lt;/script>
    &lt;script type="text/javascript" src="another one">&lt;/script>
</xsl:template>

Obviously, the first template in master.xsl is overridden by the second template in page.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 :)

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>

    &lt;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="/">
...
&lt;script src="{$workspace}/javascript/script1.js"&gt;&lt;/script&gt;
<xsl:call-template name="js-contextual"/>
 ...
</xsl:template>

<xsl:template name="js-contextual">
&lt;script src="script2.js"&gt;&lt;/script&gt;
&lt;script>var somevar = "no-param";&lt;/script&gt;
</xsl:template>

Then on somepage.xsl I define the template name again:

<xsl:template match="data">
...
</xsl:template>

<xsl:template name="js-contextual">
&lt;script src="script2.js"&gt;&lt;/script>
&lt;script&gt;var somevar = "<xsl:value-of select="$param"/>"&lt;/script&gt;
</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.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details