简体   繁体   中英

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!

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>    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM