Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitemesh custom javascript per page

I am using sitemesh for a spring based site. The problem is that I have some javascript that I want it to run on the onload event of only one specific page using jquery $(function() { ... }) and not on every page.

I have included the jquery.js in the bottom of my decorator, after the body. So, if I try to include a <script> tag in the body of my decorated page the script won't be executed because jquery will be loaded after that! I know that I could include the jquery.js in the header of my decorator so it will be before the custom script in the decorated page however I don't really like that solution since it will contain javascritp in the head of my page.

So I would like to have something like a placeholder in my sitemesh decorator in where the custom from my decorated page will be placed (as you can understand I come from the django world :p). Is this possible ? Do you propose anything else or should I just put my jquery.js in the header and be done with it ?

like image 819
Serafeim Avatar asked Feb 15 '23 02:02

Serafeim


1 Answers

To answer my question, after some search I found the following solution:

I Added the following to the end of my decorator page (after including jquery.js)

<decorator:getProperty property="page.local_script"></decorator:getProperty>

I also added the following

<content tag="local_script">
<script>
  $(function() {
    alert("Starting");
  });
</script>
</content>

The decorated result page contained the contents of the local_script tag exactly where I wanted them and everything worked fine :)

I don't know why this feature of sitemesh is not properly documented - using this you can have a great templating behaviour (like django).

like image 173
Serafeim Avatar answered Feb 17 '23 20:02

Serafeim