Call different scripts from different pages
This is an open discussion with 5 replies, filed under XSLT.
Search
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'> <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/> <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.
Hello,
I have added a call of a javascript to the end of my
body
(this call is placed in a master.xslutility
that is the basis for all pages):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).