playframework - Play Framework 2.x access variable in parent layout -


below simple main.scala.html template.

@import services.userprovider  @(userprovider: userprovider)    <!doctype html>  <html lang="@lang().code()">      <head>      </head>        @defining(userprovider.getuser(session())) { user =>      <body>          <main id="main-container">              <div class="content">                  @content              </div>          </main>      </body>      }  </html>

i have defined user variable using @defining. in of child templates inherit main.scala.html, there way access "user" variable without having make same @defining(userprovider.getuser(session()))) call repeatedly (as shown in child template below)?

@import services.userprovider  @(userprovider: userprovider, model: mymodel)    @main(userprovider, model) {        @defining(userprovider.getuser(session())) { user =>            <div class="block-content">              @views.html.foo.list._filter(user, model)          </div>            <div>              @_showingbarpartial(user, model)              @views.html.foo.list._table(model, user)          </div>      }    }

i have code snippet on project , trying determine if necessary and/or if lead problems. seems like, have defined once in main (parent) template, why not reuse instead of having reget in child templates.

thanks suggestions.

could consider using scala implicit?

from controller define

implicit val user: user = userprovider.getuser(session())

and in each template reference implicit parent closure, trick have pass reference implicit through each template:

main.scala.html

@(model: mymodel)(implicit val user: user)  @main(userprovider, model) {   <div class="block-content">     @views.html.foo(model)     ... 

foo.scala.html

@(model: mymodel)(implicit val user: user)    @user.name logged in  @* <--- using implicit in child template *@ 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -