Skip to content

Commit

Permalink
Enhance Groovy template support in CLI
Browse files Browse the repository at this point in the history
to allow user to specify the TemplateEngine

Fixes gh-908
  • Loading branch information
Dave Syer committed May 20, 2014
1 parent 7e9a403 commit 334c814
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions spring-boot-cli/samples/template.groovy
Expand Up @@ -8,6 +8,7 @@ class Example implements CommandLineRunner {
@Autowired
private MyService myService

@Override
void run(String... args) {
print template("test.txt", ["message":myService.sayWorld()])
}
Expand Down
Expand Up @@ -51,7 +51,7 @@ public void applyImports(ImportCustomizer imports) {
"org.springframework.web.servlet.config.annotation",
"org.springframework.web.servlet",
"org.springframework.web.servlet.handler", "org.springframework.http",
"org.springframework.ui");
"org.springframework.ui", "groovy.text");
imports.addStaticImport(GroovyTemplate.class.getName(), "template");
}

Expand Down
Expand Up @@ -16,11 +16,14 @@

package org.springframework.boot.groovy;

import groovy.lang.Writable;
import groovy.text.GStringTemplateEngine;
import groovy.text.Template;
import groovy.text.TemplateEngine;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
Expand All @@ -41,7 +44,15 @@ public static String template(String name) throws IOException,

public static String template(String name, Map<String, ?> model) throws IOException,
CompilationFailedException, ClassNotFoundException {
return getTemplate(name).make(model).toString();
return template(new GStringTemplateEngine(), name, model);
}

public static String template(TemplateEngine engine, String name, Map<String, ?> model)
throws IOException, CompilationFailedException, ClassNotFoundException {
Writable writable = getTemplate(name).make(model);
StringWriter result = new StringWriter();
writable.writeTo(result);
return result.toString();
}

private static Template getTemplate(String name) throws CompilationFailedException,
Expand Down

0 comments on commit 334c814

Please sign in to comment.