Twirl omitting parameters

Based on the following guide:
https://www.playframework.com/documentation/2.7.x/ScalaTemplateUseCases

I’d like to know how to omit fields like demonstrated here:

@(title: String, scripts: Html = Html(""))(content: Html)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
        <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
        @scripts
    </head>
    <body>
        <div class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container">
                    <a class="brand" href="#">Movies</a>
                </div>
            </div>
        </div>
        <div class="container">
            @content
        </div>
    </body>
</html>

Now the following are acceptable:

@scripts = {
    <script type="text/javascript">alert("hello !");</script>
}

@main("Title",scripts){

   Html content here ...

}

And

@main("Title"){

   Html content here ...

}

Now if I have the following:

@(title: String, scripts: Html = Html(""), canonicalTag: Html = Html(""))(content: Html)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
        @canonicalTag
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
        <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
        @scripts
    </head>
    <body>
        <div class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container">
                    <a class="brand" href="#">Movies</a>
                </div>
            </div>
        </div>
        <div class="container">
            @content
        </div>
    </body>
</html>

How can I implement a page without a script but with a canonical tag?

And a page without either a script or canonical tag?

Please advise,

Thanks

Hey @AlbaroPereyra, you can do that by using named arguments. For example, considering the last template you sent:

@pageScripts = {
    <script type="text/javascript">alert("hello !");</script>
}

@main("Title", scripts = pageScripts) {

   Html content here ...

}

Or:

@canTag = {
    ...
}

@main("Title", canonicalTag = canTag) {

   Html content here ...

}

Best.

I wish this would’ve resolved my problem unfortunately I am getting the following error:

Compilation error

not enough arguments for method apply: (googleTagManager: play.twirl.api.Html, title: String, metaDescription: String, canonicalTag: play.twirl.api.Html, styles: play.twirl.api.Html, googleTagManagerScript: play.twirl.api.Html, scripts: play.twirl.api.Html)(content: play.twirl.api.Html)play.twirl.api.HtmlFormat.Appendable in object main. Unspecified value parameters googleTagManager, canonicalTag, styles…

When I try to use the following main template:

@(googleTagManager: Html, title: String, metaDescription: String, canonicalTag: Html, styles: Html, googleTagManagerScript: Html, scripts: Html)(content: Html)
<html>
...
</html>

with

@scriptsVar = {
  <script type="text/javascript" src="@routes.Assets.versioned("js/getScreenDimensions.js")"></script>
}

@templates.main(title = "Example.com | Login to access our private content", metaDescription = "This is the login page enter your example.com credentials to login.", scripts = scriptsVar){
...
}

It is because you don’t have default values for a bunch of parameters. I would suggest a code like:

@(title: String, metaDescription: String)(googleTagManager: Html = Html(""), canonicalTag: Html = Html(""), styles: Html = Html(""), googleTagManagerScript: Html = Html(""), scripts: Html = Html(""))(content: Html)
<html>
...
</html>

Notice that I’m using multiple parameter lists.

So, when using the main template you will need to specify all the parameters that don’t have a default value and then override the default values for some of them if you need/want:

@scriptsVar = {
  <script type="text/javascript" src="@routes.Assets.versioned("js/getScreenDimensions.js")"></script>
}

@templates.main(title = "Page Title", metaDescription = "Some description")(scripts = scriptsVar) {
  ...
}

I completely forgot about setting the default values, thanks for bringing it up.

It works like a charm now.

Thanks.