Skip to content

Instantly share code, notes, and snippets.

@seralf
Created October 26, 2013 14:26
Show Gist options
  • Save seralf/7170034 to your computer and use it in GitHub Desktop.
Save seralf/7170034 to your computer and use it in GitHub Desktop.
A simple template scala Application to test Thymeleaf locally (without spring, and outside servlet a context)
package thymeleaf
import org.thymeleaf.TemplateEngine
import org.thymeleaf.templateresolver.TemplateResolver
import org.thymeleaf.templateresolver.FileTemplateResolver
import org.thymeleaf.resourceresolver.FileResourceResolver
import org.thymeleaf.context.Context
import java.io.Writer
import java.io.PrintWriter
import java.io.File
import org.thymeleaf.exceptions.TemplateEngineException
import org.thymeleaf.templateresolver.TemplateResolution
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
import java.text.SimpleDateFormat
import java.util.Date
object TestTemplate extends App {
val now = new SimpleDateFormat("dd MMMM YYYY - HH:mm").format(new Date())
val ctx = new Context()
ctx.setVariable("today", now)
val templateEngine = new TemplateEngine()
// SEE templateEngine.setTemplateResolver(new ClassLoaderTemplateResolver())
val resolver = new FileTemplateResolver()
resolver.setCharacterEncoding("UTF-8")
templateEngine.setTemplateResolver(resolver)
templateEngine.initialize()
val writer = new PrintWriter(System.out)
val result = templateEngine.process("html/home.html", ctx)
// VIEW RESULTS
val text = new String(result.getBytes())
println("TEXT: " + text)
}
/*
contents of: html/home.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Today is: <span th:text="${today}">13 February 2011</span></p>
</body>
</html>
*/
package it.seralf.solrbook.writers.thymeleaf
object MainThymeleafResponseWriter extends App {
import org.thymeleaf.TemplateEngine
import org.thymeleaf.templateresolver.TemplateResolver
import org.thymeleaf.templateresolver.FileTemplateResolver
import org.thymeleaf.resourceresolver.FileResourceResolver
import org.thymeleaf.context.Context
import java.io.Writer
import java.io.PrintWriter
import java.io.File
import org.thymeleaf.exceptions.TemplateEngineException
import org.thymeleaf.templateresolver.TemplateResolution
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
import java.text.SimpleDateFormat
import java.util.Date
val now = new SimpleDateFormat("dd MMMM YYYY - HH:mm").format(new Date())
val ctx = new Context()
ctx.setVariable("today", now)
val templateEngine = new TemplateEngine()
templateEngine.setTemplateResolver(new ClassLoaderTemplateResolver())
templateEngine.initialize()
val writer = new PrintWriter(System.out)
val pack = this.getClass().getPackage().getName().replace(".", "/")
val result = templateEngine.process(pack + "/home.html", ctx)
// VIEW RESULTS
val text = new String(result.getBytes())
println("TEXT:\n" + text)
}
/*
contents of: home.html in the same package of the class
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<span th:text="${x=today.toString().replaceAll('^.*?(\\d{4}).*?$', '$1')}">FIRST YEAR SNIPPET</span>
<p th:text="${'YEAR '+x}">SECOND YEAR SNIPPET, WITH VARIABLE</p>
<p id="today">Today is: <span th:text="${today}">13 February 2011</span></p>
</body>
</html>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment