FranciscoBouza
FranciscoBouza

Reputation: 620

How to add HTML content in both head and body sections in Play Framework?

While using Play Framework 2, I often use a "master page" (main.scala.html) where I leave part of the body to be added later by another page (let's say content.scala.html).

It has happened to me (several times) that page I want to add some HTML to the head (such as .css files) in a concrete page which uses the main. How could I call the "master page" with both HTML parts?

Thanks in advance!

Upvotes: 2

Views: 342

Answers (1)

Salem
Salem

Reputation: 12986

You can add an additional parameter at the end in the main file (sample in documentation)

@(param1: String, moreHeaders = Html(""))(content)
<html>
    <head>
        <!-- Your default headers here -->
        @moreHeaders
    </head>
    <body>
        @content
    </body>
</html>

and then in the files you need to add additional headers you define them using a variable:

@moreHeaders = {
    <script src="path/to/file.js"></script>
    <!-- (...) -->    
}

@main("First parameter", moreHeaders) {
    <p>Hi</p>    
}

As the moreHeaders has a default value, when you dont want to add additional headers you can just omit it:

@("First parameter") {
    <p>Hi</p>    
}

Upvotes: 3

Related Questions