Search in sources :

Example 11 with Handlebars

use of com.github.jknack.handlebars.Handlebars 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 12 with Handlebars

use of com.github.jknack.handlebars.Handlebars 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 13 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project cw-omnibus by commonsguy.

the class WebServerService method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    ConnectivityManager mgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo ni = mgr.getActiveNetworkInfo();
    if (ni == null || ni.getType() == ConnectivityManager.TYPE_MOBILE) {
        EventBus.getDefault().post(new ServerStartRejectedEvent());
        stopSelf();
    } else {
        handlebars = new Handlebars(new AssetTemplateLoader(getAssets()));
        rootPath = "/" + new BigInteger(20, rng).toString(24).toUpperCase();
        server = new AsyncHttpServer();
        if (configureRoutes(server)) {
            server.get("/.*", new AssetRequestCallback());
        }
        server.listen(getPort());
        raiseReadyEvent();
        foregroundify();
        timeoutFuture = timer.schedule(onTimeout, getMaxIdleTimeSeconds(), TimeUnit.SECONDS);
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) Handlebars(com.github.jknack.handlebars.Handlebars) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) BigInteger(java.math.BigInteger)

Example 14 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project ratpack by ratpack.

the class HandlebarsModule method provideHandlebars.

@SuppressWarnings("UnusedDeclaration")
@Provides
@Singleton
Handlebars provideHandlebars(Config config, Injector injector, TemplateLoader templateLoader, TemplateCache templateCache) {
    final Handlebars handlebars = new Handlebars().with(templateLoader).with(templateCache).startDelimiter(config.getStartDelimiter()).endDelimiter(config.getEndDelimiter());
    GuiceUtil.eachOfType(injector, NAMED_HELPER_TYPE, helper -> handlebars.registerHelper(helper.getName(), helper));
    return handlebars;
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 15 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project ddf by codice.

the class FeedbackApplication method initConfigurationAppValues.

private void initConfigurationAppValues(boolean reinit) {
    Handlebars handlebars = new Handlebars();
    if (reinit || subjectTemplate == null || bodyTemplate == null) {
        if (configurationApplication != null) {
            emailDestination = configurationApplication.getQueryFeedbackEmailDestination();
            String subjectTemplateStr = configurationApplication.getQueryFeedbackEmailSubjectTemplate();
            String bodyTemplateStr = configurationApplication.getQueryFeedbackEmailBodyTemplate();
            try {
                if (subjectTemplateStr != null) {
                    subjectTemplate = handlebars.compileInline(subjectTemplateStr);
                }
                if (bodyTemplateStr != null) {
                    bodyTemplate = handlebars.compileInline(bodyTemplateStr);
                }
            } catch (IOException e) {
                LOGGER.warn("Unable to compile email templates", e);
            }
        } else {
            LOGGER.debug("Feedback configuration is not set");
        }
    }
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) IOException(java.io.IOException)

Aggregations

Handlebars (com.github.jknack.handlebars.Handlebars)24 IOException (java.io.IOException)13 Template (com.github.jknack.handlebars.Template)10 ClassPathTemplateLoader (com.github.jknack.handlebars.io.ClassPathTemplateLoader)8 Context (com.github.jknack.handlebars.Context)6 FileTemplateLoader (com.github.jknack.handlebars.io.FileTemplateLoader)6 TemplateLoader (com.github.jknack.handlebars.io.TemplateLoader)3 Date (java.util.Date)3 UIContext (com.github.bordertech.wcomponents.UIContext)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 Provides (com.google.inject.Provides)2 Singleton (com.google.inject.Singleton)2 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)2 Placemark (de.micromata.opengis.kml.v_2_2_0.Placemark)2 TimeSpan (de.micromata.opengis.kml.v_2_2_0.TimeSpan)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 DateFormat (java.text.DateFormat)2