Why use the template engine in playframework 2 (scala) if we can stay with pure scala

Why use the scala template-engine in playframework 2 (scala) if we can only stay with scala .

Using the template engine:

  • optional processor that converts template syntax to scala code
  • then we compile this code (which is not as short as if it were written manually), then it compiles even slower)
  • Also, if the template has not been converted to scala yet, you can see the code inconsistency (red highlight in your IDE) from the main code - so you should think about it every time.

Why not just use the xml / html kernel support that scala offers here: http://www.scala-lang.org/node/131

Is there any clean scala template (you can recommend), can I use it in the play-framework or alone?

+4
source share
2 answers

For me, this is suitable as an answer, at least for the last question.

It is just scala. Just the built-in magic of XML.

http://www.alvarocarrasco.com/2011/03/play-framework-and-templating-with.html?m=1

Example:

This is the template : Templates.scala file

 package templates import play.api.templates.Html import scala.xml.Xhtml import controllers.routes object Main { def page (title:String="Default title")(content: => scala.xml.Elem) = Html { "<!DOCTYPE html>" + Xhtml.toXhtml( <html> <head> <title>{title}</title> <link rel="stylesheet" media="screen" href={routes.Assets.at("stylesheets/main.css").toString()} /> <link rel="shortcut icon" type="image/png" href={routes.Assets.at("images/favicon.png").toString()} /> <script src={routes.Assets.at("javascripts/jquery-1.9.0.min.js").toString()} type="text/javascript" /> </head> <body> {content} </body> </html> ) } // a panel template, just as an example def panel (label:String="Some label")(content: => scala.xml.Elem) = { <div class="panel"> <div class="panel-label">{label}</div> <div>{content}</div> </div> } } 

This is the index.scala file index page .

 package views import templates.Main._ object IndexPage { def apply() = { page(title="Welcome to my Page!") { <div> <h1>Hello</h1> <p>Some template markup</p> { panel(label="Dashboard panel")( <div> Panel content </div> ) } </div> } } } 

This is the controller: Application.scala File

 package controllers import play.api.mvc._ object Application extends Controller { def index = Action { Ok( views.IndexPage() ); } } 
+2
source

You should actually ask this question to the development team, however, consider a few points:

  • Actually, you don’t need to use the Templating engine at all for playback, you can easily return any line using the Ok() method, so according to your link, you can just do something like Ok(theDate("John Doe").toString())
  • Play uses an approach that is very typical of other MVC web frameworks, where views are HTML-based files because ... it's a web-based environment. I don’t see anything wrong with that, sometimes I work with other languages ​​/ frameworks and see that only the difference in representations between them is just a syntax specific to the language that targets!
  • Don’t forget that Play is a bilingual system, someone may ask: β€œWhy not use some Java library to handle views?”
  • Scala built-in XML literals are not suitable for creating complex programs, you easily run into problems (which also explains why there is a library called anti-xml ); Martin Odersky himself regretted that it was a language.
  • Finally, there is an IDE with support for Play 2 representations, I am working on an Idea 12 with Play2 support, and although this is not ideal (this is completely new, so sometimes there are small problems), in most cases it works fine. He understands the syntax of Play view, offers autocomplete, even you can use the option + click on any object in the view to go directly to the declaration of the method / model, etc.

Answering your last question, AFAIK officially contains the Groovy engine available as a module that offers a template engine known from Play 1.x, however, keep in mind that this is just a bridge for people moving from Play 1.x to Play 2. x, as it is only slower than its own Play 2 engine.

+3
source

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


All Articles