Reputation: 3072
I have a Scala template like this...
@()
import view.html.partials._header
import view.html.partials._footer
<!DOCTYPE html>
<html lang="en">
@_header()
/* Body of web page */
@_footer
</html>
Every single page has the same header and footer and a different body. I do not want to do this...
Page #1...
@()
import view.html.partials._header
import view.html.partials._footer
import view.html.partials._body1
<!DOCTYPE html>
<html lang="en">
@_header()
@_body1()
@_footer
</html>
Page #2...
@()
import view.html.partials._header
import view.html.partials._footer
import view.html.partials._body2
<!DOCTYPE html>
<html lang="en">
@_header()
@_body2()
@_footer
</html>
Page #3...
@()
import view.html.partials._header
import view.html.partials._footer
import view.html.partials._body3
<!DOCTYPE html>
<html lang="en">
@_header()
@_body3()
@_footer
</html>
Etc.
Is there a way to pass in the name of the partial template you want to render as a parameter? What is the solution to this problem?
p.s. I don't see the solution in... the play template documentation
Upvotes: 0
Views: 416
Reputation: 12214
Instead of having all these repetitions, you can create a main.scala.html
file to be used as a default layout:
@(title: String)(content: Html)
@import view.html.partials._header
@import view.html.partials._footer
<!DOCTYPE html>
<html lang="en">
@_header()
<body>
@content
@_footer()
</body>
</html>
The first line says exactly "this view will receive a title argument and also a block of HTML". From that, you can do the following:
Page #1:
@(someParameter: String)
@main("The title of Page #1") {
<h1>Hello, I'm the body of Page #1</h1>
<p>As you can see, I'm calling the main view passing
a title and a block of HTML</p>
}
Page #2:
@(someParameter: String, anotherParameter: Long)
@main("This time Page #2") {
<h1>Hello, I'm the body of Page #2</h1>
<p>Just like Page #1, I'm passing a title
and a block of HTML to the main view.</p>
}
This is all explained at the docs, but in another page:
https://www.playframework.com/documentation/2.0/ScalaTemplateUseCases
Upvotes: 2
Reputation: 3072
Make a switch or case statement like this...
@(bodyCase: ClosedEnumType)
import view.html.partials._header
import view.html.partials._footer
import view.html.partials._body1
import view.html.partials._body2
<!DOCTYPE html>
<html lang="en">
@_header()
@bodyCase match {
case Body1() => {
@_body1()
}
case Body2() => {
@_body2()
}
}
@_footer
</html>
Upvotes: -1