- Notifications
You must be signed in to change notification settings - Fork5
dmitraver/scala-play-thymeleaf-plugin
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A module for Play Framework(Scala) to use [Thymeleaf] (http://thymeleaf.org) template engine as an alternative to [Twirl] (https://github.com/playframework/twirl), which is a standard Play Framework template engine.
Thymeleaf is anXML / XHTML / HTML5 template engine that is suited for serving XHTML/HTML5 at the view layer of web applications. It is based on XML tags and attributes that define the execution of predefined logic on the DOM (Document Object Model), instead of explicitly writing that logic as code inside the template.
To use this module add it as a dependency to your build file
"com.github.dmitraver" %% "play-thymeleaf-plugin" % "1.0"
To see this module in action check out the project and run sample with the following commands:
cd project_sources_rootsbt "project sample" run
Sample represents a simple web application which is a remastered version of "The Good Thymes Virtual Grocery" - original web application created by Thymeleaf authors to show capabilites of template engine.
Module allows to override some Thymeleaf configuration options. You can override default values by specifying them inapplication.conf file. The following configuration options with their defualt values are available:
Enables Thymleaf caching capability for templates.
Template cache ttl in milliseconds. If not set, entries would be cached until expelled by LRU.
Defines type of templates that Thymeleaf can process.
Defines a folder where you should place your template files.
Defines template files extension.
Please refer to Thymeleaf documentation about possible configuration values.
The central class of this module isThymeleaf object which has one method with the following signature:
def render(templateName: String, templateObjects: Map[String, AnyRef] = Map())(implicit language: Lang, flash: Flash = Flash(), session: Session = Session()): Html
where
- templateName is a name of your template. By default module assumes that the templates are located underpublic/ directory of Play application which can be overriden by specifyingthymeleaf.template.prefix property inapplication.conf file.
- templateObjects are a Map of named objects that can be used inside template.
This method also takes some implicit parameters which can be provided by specifying implicit request parameter in controller action method. For example:
def ordersList() = Action { implicit request => val orders = Order.findAll()Ok(Thymeleaf.render("order/list", Map("orders" -> orders)))}
You can use standard Play FrameworkForm object to map and validate your form data. However, there is a special form helper classFormWrapper which can simplify working with forms and provides some methods to get error messages inside your templates. It's advisable always to wrap your form intoFormWrapper before passing them as a template objects.Example:
def register() = Action { implicit request => Ok(Thymeleaf.render("register", Map("userForm" -> FormWrapper(registrationForm))))}
Themeleaf uses OGNL(Object-Graph Navigation Language) as a standard template expression language. OGNL requires classes to have getter methods in order for them to be accessible inside templates. However, you can omit this requirement when usingcase classes which can be accessible without providing getter methods for them.
About
Thymeleaf template engine plugin for Play(Scala) framework
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.