Git Product home page Git Product logo

scala-play-thymeleaf-plugin's Introduction

Thymleaf module for Play Framework 2.x(Scala)

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 an XML / 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.

Installation

To use this module add it as a dependency to your build file

"com.github.dmitraver" %% "play-thymeleaf-plugin" % "1.0"

Sample application

To see this module in action check out the project and run sample with the following commands:

cd project_sources_root
sbt "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.

Configuration

Module allows to override some Thymeleaf configuration options. You can override default values by specifying them in application.conf file. The following configuration options with their defualt values are available:

thymeleaf.cache.enabled=false

Enables Thymleaf caching capability for templates.

thymeleaf.cache.ttlms=null

Template cache ttl in milliseconds. If not set, entries would be cached until expelled by LRU.

thymeleaf.template.mode=XHTML

Defines type of templates that Thymeleaf can process.

thymeleaf.template.prefix=public/

Defines a folder where you should place your template files.

thymeleaf.template.suffix=.html

Defines template files extension.

Please refer to Thymeleaf documentation about possible configuration values.

Usage

The central class of this module is Thymeleaf 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 under public/ directory of Play application which can be overriden by specifying thymeleaf.template.prefix property in application.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)))
}

Forms

You can use standard Play Framework Form object to map and validate your form data. However, there is a special form helper class FormWrapper 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 into FormWrapper before passing them as a template objects. Example:

def register() = Action { 
    implicit request =>
        Ok(Thymeleaf.render("register", Map("userForm" -> FormWrapper(registrationForm))))
}

Accessing template objects

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 using case classes which can be accessible without providing getter methods for them.

scala-play-thymeleaf-plugin's People

Contributors

dmitraver avatar seralf avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.