Skip to content

Instantly share code, notes, and snippets.

@a-thomas
Created May 12, 2012 19:33
Show Gist options
  • Save a-thomas/2668407 to your computer and use it in GitHub Desktop.
Save a-thomas/2668407 to your computer and use it in GitHub Desktop.
public class Application {
public static void main(String[] args) throws IOException {
final Configuration cfg = configureFreemarker();
get(new Route("/") {
@Override
public Object handle(Request request, Response response) {
//freemarker needs a Writer to render the final Html code
StringWriter sw = new StringWriter();
//params used in the template files
//passed the sublayout filename and the title page
HashMap<String, String> params = getPageParams("home.ftl", "Home page");
try {
//template engine processing
cfg.getTemplate("main.ftl").process(params, sw);
} catch (Exception e) {
e.printStackTrace();
}
//return the rendered html code
return sw.toString();
}
});
}
private static Configuration configureFreemarker() {
Configuration cfg = new Configuration();
try {
//indicates the templates directory to freemarker
cfg.setDirectoryForTemplateLoading(new File("templates"));
} catch (IOException e) {
e.printStackTrace();
}
return cfg;
}
//uses to create a Hashmap with specific keys
private static HashMap<String, String> getPageParams(String page, String title) {
HashMap<String, String> params = new HashMap<String, String>();
//page and title from main.ftl
params.put("page", "pages/" + page);
params.put("title", title);
return params;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment