Search

Hello,

I have added a call of a javascript to the end of my body (this call is placed in a master.xsl utility that is the basis for all pages):

 <script type="text/javascript" src="/workspace/js/domscript.js"></script>
<script>jQuery(slideshow); jQuery(gestecke);</script>
</body>
</html>

Now I need to call different scripts from different pages but I also want to keep the master if possible. How could I achieve this?

Thank you! Moritz

PS: And how on earth can I stop this editor from changing my < to & lt; ? The help link for the editor is broken (wmd-editor.com).

Pretty simple :)

In your master use this

<xsl:apply-templates select='*' mode='javascript'/>

Then create ta template (in your master or a linked file)

<xsl:template match='*' mode='javascript'>
    &lt;script src='test.js'/>
</xsl:template>

For Each page where you want to add more scripts do the following in the page.xsl

<xsl:template match='*' mode='javascript'>
    <xsl:apply-imports/>
    &lt;script src='test.js'/>
</xsl:template>

The apply imports will run your master scripts; then you can add the new ones. Should you not want to include your 'master' scripts just skip the apply-imports

sorry Im not able to post code, testing later..


<script type="text/javascript" >
 
</script>

Hello gunglien,

thank you for your solution (which works, with one drawback). I am sorry, it took me a while to check it out and to write back.

I had tried before to call a named template. My master xsl had this line

<xsl:apply-templates select='javascript' />

It did not work; it seems one cannot call a named template from the master? So Do I understand correctly that your trick is to call the whole node (wildcard) but with the restriction it has to have the mode 'javascript'? Why does this work and a named template does not? Thanks for helping further understand xsl.

Your solution produces a faulty page when I have no <xsl:template match='*' mode='javascript'> in my page xsl. Then the whole xml is inserted (of course). Is there a way to to an xsl:if that checks for the existance of the mode='javascript' part?

Thanks a lot Moritz

github: https://github.com/strlcp/symphony/blob/master/workspace/utilities/flexible-js-inc.xsl

hope this helps.

Moritz the trick is very simple; in your master template make sure to always include a template even if its empty.

<xsl:template match='*' mode='javascript'></xsl:template>

So if you require no scripts it would still work. I would reccomend that you would put your common scripts in there (if you have any) then make use of <xsl:apply-imports/> whenever you need to add/extend to them. I use this structure for my sites nowadays, its quite convenient.

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