Darbs
Darbs

Reputation: 163

include scala.html files in play 2.0 scala

I am trying to learn Play 2.0 with scala but I dont think i quite understand how the template system for play 2.0 works. I have used play 1.2 before and i am sort of looking for an equivalent to the #{include 'views/blah.html' /}. I essentially want to create a navbar that is rendered on all the pages.

Essentially in main.scala.html i have

@(title: String)(navbar: 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>
  </head>
  <header>
    This is my header
  </header>
    <section class="navbar">@navbar</section>
    <section class="content">@content</section>
  <footer>
    This is my footer
  </footer>

and in my index.scala.html:

@navbar = {  
<h1>Index</h1>
<ul>
    <li> 
        <a [email protected]>Tasks</a>
    </li>
</ul>
}
@main("Home")(navbar){
  content
}

in task.scala.html:

@(tasks: List[Task], taskForm: Form[String]) 
@import helper._
@main("Home") {
<h1>Index</h1>
<ul>
    <li> 
        <a [email protected]>Tasks</a>
    </li>
</ul>
} {
  task code
}

Now to include this navbar it seems i have to repeat this in every page this way i would have to hard code this navbar into every page. Is there a way to do this without without writing the whole navbar in every page?

I have also tried creating a navbar.scala.html file that contains

<h1>Index</h1>
<ul>
    <li> 
        <a [email protected]>Tasks</a>
    </li>
</ul>

and saving under views/ then importing that using @import views.navbar but then i get an error stating 'navbar is not a member of views'. I am writing this in Eclipse Java EE IDE indigo if that helps.

Upvotes: 6

Views: 7805

Answers (2)

Chad
Chad

Reputation: 11

To include any other views template into another views template, you simple call it using: @views.html.[location].[location].[location]()

Where [location] is just a break down of it's path.

for example:

@views.html.users.interface()

Be sure to put the "()" ie the brackets at the end of the statement if it does not take any parameters. Without the "()" you will get an error message like this: "BaseScalaTemplate(play.api.templates...)"

If your template has parameters, be sure to include them when you call it, like this:

@views.html.users.interface( "name" )

Upvotes: 0

Julien Richard-Foy
Julien Richard-Foy

Reputation: 9663

Dont import it but just call it:

@navbar()

Upvotes: 7

Related Questions