Thang Nguyen
Thang Nguyen

Reputation: 1151

Playframework - Append JS outside the main.html

In Play Framework, it's usually have main.html with code like:

<html>
    <head>
        <title>#{get 'title' /}</title>     
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" type="text/css" media="screen" 
            href="@{'/public/stylesheets/main.css'}" />
        <link rel="shortcut icon" type="image/png" 
            href="@{'/public/images/favicon.png'}" />
    </head>
    <body>
        #{doLayout /} 
    </body>
</html>

So when I have a view such as index.html, it's usually start with

#{extends 'main.html' /}

And the index.html only contain the body part ( #{doLayout /} in main.html ).

I want to add javascript depend on page loaded (in page coffee.html I want to load water.js for example). Now I have to add it in main.html and loaded it all over the application.

1/ How can it be loaded from extended view (index.html)?

2/ In addition, how can I get the baseUrl value (like Zend framework) to pass to javascript variable?

Upvotes: 1

Views: 728

Answers (1)

Codemwnci
Codemwnci

Reputation: 54924

You can do this with set and get tags.

If your main.html is like this

<title>#{get 'title' /}</title>     
#{get 'moreScripts' /}

Then in your different views you can set different JS. For example, index.html may have no JS, but coffee.html will have

#{set 'moreScripts'}
    <script src="@{'/public/javascripts/yourscript.js'}" type="text/javascript" charset="utf-8"></script>
#{/set}

Upvotes: 2

Related Questions