use of com.github.jknack.handlebars.Context in project ballerina by ballerina-lang.
the class ModelGenerator method generateSourceFiles.
public static void generateSourceFiles(JsonObject node, String tpl, String name) {
try {
String templateString = FileUtils.readFileToString(new File(TEMPLATE_PATH + tpl), "UTF-8");
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline(templateString);
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Object>>() {
}.getType();
Map<String, Object> map = gson.fromJson(node, type);
Context context = Context.newBuilder(map).build();
String generated = template.apply(context);
FileUtils.writeStringToFile(new File(GENERATE_PATH + name), generated, "UTF-8");
} catch (IOException e) {
throw new AssertionError("Template files should be always defined.");
}
}
use of com.github.jknack.handlebars.Context in project knotx by Cognifide.
the class JsonObjectValueResolverTest method JsonObjectResolver_whenApplyingFileBasedObject_expectVariablesResolved.
@Test
public void JsonObjectResolver_whenApplyingFileBasedObject_expectVariablesResolved() throws Exception {
Context context = Context.newBuilder(filebasedModel()).push(JsonObjectValueResolver.INSTANCE).build();
String compiled = template.apply(context).trim();
assertThat(compiled, equalTo(expected));
}
use of com.github.jknack.handlebars.Context in project wcomponents by BorderTech.
the class HandlebarsRendererImpl method renderTemplate.
/**
* {@inheritDoc}
*/
@Override
public void renderTemplate(final String templateName, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
LOG.debug("Rendering handlebars template " + templateName);
try {
// Map the tagged components to be used in the replace writer
Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
// Get Engine
Handlebars handlebars = getHandlebarsEngine(options);
// Load template (Handlebars loader makes the template name "absolute")
Template template = handlebars.compile(templateName);
// Setup handlebars context
Context handlebarsContext = createContext(context);
// Render
writeTemplate(template, handlebarsContext, componentsByKey, writer);
} catch (FileNotFoundException e) {
throw new SystemException("Could not find handlebars template [" + templateName + "]. " + e.getMessage(), e);
} catch (Exception e) {
throw new SystemException("Problems with handlebars template [" + templateName + "]. " + e.getMessage(), e);
}
}
use of com.github.jknack.handlebars.Context in project wcomponents by BorderTech.
the class HandlebarsRendererImpl method renderInline.
/**
* {@inheritDoc}
*/
@Override
public void renderInline(final String templateInline, final Map<String, Object> context, final Map<String, WComponent> taggedComponents, final Writer writer, final Map<String, Object> options) {
LOG.debug("Rendering handlebars inline template.");
try {
// Map the tagged components to be used in the replace writer
Map<String, WComponent> componentsByKey = TemplateUtil.mapTaggedComponents(context, taggedComponents);
// Get Engine
Handlebars handlebars = getHandlebarsEngine(options);
// Compile inline
Template template = handlebars.compileInline(templateInline);
// Setup handlebars context
Context handlebarsContext = createContext(context);
// Write template
writeTemplate(template, handlebarsContext, componentsByKey, writer);
} catch (Exception e) {
throw new SystemException("Problems with handlebars inline template. " + e.getMessage(), e);
}
}
use of com.github.jknack.handlebars.Context in project vertx-web by vert-x3.
the class HandlebarsTemplateEngineImpl method render.
@Override
public void render(RoutingContext context, String templateDirectory, String templateFileName, Handler<AsyncResult<Buffer>> handler) {
try {
String baseTemplateFileName = templateFileName;
templateFileName = templateDirectory + templateFileName;
Template template = isCachingEnabled() ? cache.get(templateFileName) : null;
if (template == null) {
synchronized (this) {
loader.setPrefix(templateDirectory);
loader.setVertx(context.vertx());
// Strip leading slash from Utils##normalizePath
template = handlebars.compile(baseTemplateFileName.substring(1));
if (isCachingEnabled()) {
cache.put(templateFileName, template);
}
}
}
Context engineContext = Context.newBuilder(context.data()).resolver(getResolvers()).build();
handler.handle(Future.succeededFuture(Buffer.buffer(template.apply(engineContext))));
} catch (Exception ex) {
handler.handle(Future.failedFuture(ex));
}
}
Aggregations