0
votes

i have a few questions about Templates. I think i didn't understand exactly how they work. Let's say I have a Website which consists of the navigation on the left side, a display in the middle and a header with additional navigation. So I have the following templates:

  • main.scala.html
  • navigation.scala.html
  • header.scala.html
  • display.scala.html

main.scala.html contains the general structure of the site. It gets the navigation, header and display as parameters. So the first line in the main would be:

@(navigation: html)(header: html)(display: html)

Am I right?

If I want to view the whole page, I will just call ok(main.render()) in my controller.

However, how can I change the navigation on my website? When I create a new template, lets call it newNavigation.scala.html and I call ok(newNavigation.render()) in my controller, I'll get the problem, that I only pass the newNavigation template to the main and the main misses the header and the display.

The navigation template as well as the header and display look like:

@main{
...
}

And how can I load a different display and navigation on the same time?

I'm sorry for my English and I hope someone could help me, thank you.

1
Are these Play templates? Tagging is important to make sure the right people see your question. - Dylan

1 Answers

0
votes

You need to wrap your templates the other way around. Your main.scala.html is a good starting point. But you would rather want a signature like

@(navigation: html, header: html)(display: html)

You will see why thats going to work better in a second. Now you need to define the navigation. But instead of wrapping the navigation into the main just define the navigation on its own:

@()
<ul> <li> ... </li> </ul>

Do the same for the header. After that you can create a index.scala.html which is going to put all the pieces together:

@(someName: String)
@main(navigation(), header()){
  <h1>Hello @someName</h1>
}

In a different template you can use a different navigation bar. Let's create a goodby.scala.html:

@(someName: String)
@main(newNavigation(), header()){
  <h1>Goodby @someName</h1>
}

You can also make the navigation dynamic and pass it into the template:

@(someName: String, customNavigation: html)
@main(customNavigation(), header()){
  <h1>...</h1>
}

The important point to remember: Define your snippets on their own. Define the "skeleton" (main.scala.html) and use another template to put all the pieces together.