Search in sources :

Example 1 with Context

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.");
    }
}
Also used : Context(com.github.jknack.handlebars.Context) Handlebars(com.github.jknack.handlebars.Handlebars) Gson(com.google.gson.Gson) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template) Type(java.lang.reflect.Type) JsonObject(com.google.gson.JsonObject) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Context

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));
}
Also used : Context(com.github.jknack.handlebars.Context) Test(org.junit.Test)

Example 3 with Context

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);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Context(com.github.jknack.handlebars.Context) UIContext(com.github.bordertech.wcomponents.UIContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) Handlebars(com.github.jknack.handlebars.Handlebars) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SystemException(com.github.bordertech.wcomponents.util.SystemException) Template(com.github.jknack.handlebars.Template)

Example 4 with Context

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);
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Context(com.github.jknack.handlebars.Context) UIContext(com.github.bordertech.wcomponents.UIContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) Handlebars(com.github.jknack.handlebars.Handlebars) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SystemException(com.github.bordertech.wcomponents.util.SystemException) Template(com.github.jknack.handlebars.Template)

Example 5 with Context

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));
    }
}
Also used : Context(com.github.jknack.handlebars.Context) RoutingContext(io.vertx.ext.web.RoutingContext) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template)

Aggregations

Context (com.github.jknack.handlebars.Context)12 Template (com.github.jknack.handlebars.Template)10 IOException (java.io.IOException)8 Handlebars (com.github.jknack.handlebars.Handlebars)6 PrintWriter (java.io.PrintWriter)3 UIContext (com.github.bordertech.wcomponents.UIContext)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 ClassPathTemplateLoader (com.github.jknack.handlebars.io.ClassPathTemplateLoader)2 FileTemplateLoader (com.github.jknack.handlebars.io.FileTemplateLoader)2 FileNotFoundException (java.io.FileNotFoundException)2 Test (org.junit.Test)2 TemplateLoader (com.github.jknack.handlebars.io.TemplateLoader)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Type (java.lang.reflect.Type)1