Skip to content

Instantly share code, notes, and snippets.

@insin
Created October 7, 2012 07:24
Show Gist options
  • Save insin/3847411 to your computer and use it in GitHub Desktop.
Save insin/3847411 to your computer and use it in GitHub Desktop.
Play Framework + jade4j
package controllers;
import play.*;
import play.mvc.*;
public class Application extends JadeController {
public static Result index() {
return ok(render("index", "message", "Hello world!"));
}
}
import sbt._
import Keys._
import PlayProject._
object ApplicationBuild extends Build {
val appName = "JadeApp"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
"de.neuland" % "jade4j" % "0.2.15"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
resolvers += "jade4j-releases" at "https://github.com/neuland/jade4j/raw/master/releases"
)
}
!!! 5
html(lang='en')
head
meta(charset='utf-8')
title= message
body
h1= message
package controllers;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import play.api.templates.Html;
import play.mvc.Controller;
import de.neuland.jade4j.Jade4J;
import de.neuland.jade4j.JadeConfiguration;
import de.neuland.jade4j.template.FileTemplateLoader;
import de.neuland.jade4j.template.JadeTemplate;
public class JadeController extends Controller {
private static final JadeConfiguration jade = new JadeConfiguration();
static {
jade.setTemplateLoader(new FileTemplateLoader("app/views/", "UTF-8"));
jade.setMode(Jade4J.Mode.HTML);
jade.setPrettyPrint(true);
}
/**
* Convenience method for rendering a template by name (without .jade
* extension).
*/
public static Html render(String template) {
return render(template, new HashMap<String, Object>());
}
/**
* Convenience method for rendering a template by name (without .jade
* extension) and some context variables.
*/
public static Html render(String template, Map<String, Object> context) {
try {
JadeTemplate jadeTemplate = jade.getTemplate(template);
return new Html(jade.renderTemplate(jadeTemplate, context));
}
catch(IOException e) {
throw new RuntimeException(e);
}
}
/**
* Convenience method for rendering a template by name (without .jade
* extension) and some context variables pass as varargs as pairs of
* [name] [value] arguments.
*/
public static Html render(String template, Object... contextArgs) {
assert contextArgs.length >=2 && contextArgs.length % 2 == 0;
Map<String, Object> context = new HashMap<String, Object>();
for (int i = 0; i < contextArgs.length; i += 2) {
context.put((String)contextArgs[i], contextArgs[i + 1]);
}
return render(template, context);
}
}
@inetfuture
Copy link

It seems to me that this doesn't work in production mode, the template loader will can not find files, is that right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment