Include scala.html files in scala 2.0 game

I am trying to learn Play 2.0 using scala, but I don’t think I understand how the template system for game 2.0 works. I have used game 1.2 before, and I'm kind of looking for the equivalent of # {include 'views / blah.html' /}. I really want to create a navigation bar that appears on all 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 href=@routes.Application.tasks >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 href=@routes.Application.tasks >Tasks</a> </li> </ul> } { task code } 

Now, to enable this navigation bar, it seems to me that I have to repeat this on every page in such a way, I will have to hardcode this navigation bar on each page. Is there a way to do this without writing the entire navigation bar on each page?

I also tried creating a navbar.scala.html file containing

 <h1>Index</h1> <ul> <li> <a href=@routes.Application.tasks >Tasks</a> </li> </ul> 

and saving to the view / then importing using @import views.navbar , but then I get the error message “navbar is not a member of the views”. I write this in the Eclipse Java EE Indigo IDE if this helps.

+6
source share
2 answers

Do not import it, but simply name it:

 @navbar() 
+7
source

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

Where [location] is just a breakdown of this path.

eg:

 @views.html.users.interface() 

Be sure to put "()" i.e. brackets at the end of the instruction if it does not accept any parameters. Without "()", you will receive an error message: "BaseScalaTemplate (play.api.templates ...)"

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

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

Source: https://habr.com/ru/post/915933/


All Articles