dependency-injectionplayframeworktwirl

Play Framework: Dependency Injection in Sub-Templates


I want to use Scala Templates Dependency Injection.

Using it on the entry page it works:

@this(assets: AssetsFinder)
@(projConfig: ProjectConfig)

@main(projConfig) (assets){
  <h1>Sidebar</h1>

} {
  <h1>Home page</h1>
}

But if I want to this in a sub-template like main it does NOT.

@this(assets: AssetsFinder)
@(projConfig: ProjectConfig)(content:Html)

<!DOCTYPE html>
<html>
    <head>
        <title>@projConfig.pageTitle</title>
    </head>
    <body>
        @content
    </body>
</html>

Is this not possible or do I miss something?


Solution

  • Specify sub-template as a parameter in outer template's @this parameter list. For example, assuming full name of main is views.html.main, we have

    @this(assets: AssetsFinder, main: views.html.main)
    @(projConfig: ProjectConfig)
    
    @main(projConfig) {
      <h1>Sidebar</h1>
    
    } {
      <h1>Home page</h1>
    }