/** * Each helper must be registered with a unique name. If there are more * helpers registered with the same name an {@link IllegalArgumentException} * is thrown. Use {@link #registerHelpers(Map, boolean)} to overwrite the * helpers. * * @param helpers * @return self */ public MustacheEngineBuilder registerHelpers(Map<String, Helper> helpers) { return registerHelpers(helpers, false); }
/** * * @param type * @return <code>true</code> if the tag content must be validated, * <code>false</code> otherwise * @since 1.5 */ public static boolean contentMustBeValidated(MustacheTagType type) { return contentMustBeNonWhitespaceCharacterSequence(type); }
private Mustache locateAndParse(String templateId) { Reader reader = null; try { reader = locate(templateId); if (reader == null) { return null; } return parse(templateId, reader); } finally { closeReader(reader, templateId); } }
PippoSettings pippoSettings = getPippoSettings(); MustacheEngineBuilder builder = MustacheEngineBuilder.newBuilder(); builder.setLocaleSupport(localeSupport); builder.setProperty(EngineConfigurationKey.DEFAULT_FILE_ENCODING, PippoConstants.UTF8); builder.registerHelper("ng", new AngularJsHelper()); builder.registerHelper("i18n", new I18nHelper(application.getMessages())); builder.registerHelper("formatTime", new DateTimeFormatHelper()); builder.registerHelper("prettyTime", new PrettyTimeHelper()); builder.registerHelper("webjarsAt", new WebjarsAtHelper(router)); builder.registerHelper("publicAt", new PublicAtHelper(router)); builder.registerHelpers(HelpersBuilder.extra().build()); builder.addTemplateLocator(new ClassPathTemplateLocator(10, pathPrefix, MUSTACHE)); builder.setProperty(EngineConfigurationKey.DEBUG_MODE, true); } else { builder.addMustacheListener(Minify.htmlListener()); builder.addGlobalData("contextPath", router.getContextPath()); builder.addGlobalData("appPath", router.getApplicationPath()); engine = builder.build();
/** * Builds the engine instance. * * @return the built engine */ public synchronized MustacheEngine build() { MustacheEngine engine = new DefaultMustacheEngine(this); for (EngineBuiltCallback callback : engineReadyCallbacks) { callback.engineBuilt(engine); } BuildInfo buildInfo = BuildInfo.load(); LOGGER.info("Engine built {} ({})", buildInfo.getVersion(), buildInfo.getTimestampDate()); LOGGER.debug("Engine configuration: {}", engine.getConfiguration().getInfo()); isBuilt = true; return engine; }
template = engine.getMustache(getLocalizedTemplateName(templateName, locale.toString())); if (template == null) { template = engine.getMustache(getLocalizedTemplateName(templateName, locale.getLanguage())); template = engine.getMustache(templateName);
/** * Each helper must be registered with a unique name. If there are more * helpers registered with the same name an {@link IllegalArgumentException} * is thrown. Use {@link #registerHelper(String, Helper, boolean)} to * overwrite the helper. * * @param name * @param helper * @return self */ public MustacheEngineBuilder registerHelper(String name, Helper helper) { return registerHelper(name, helper, false); }
public void invalidateTemplateCache() { if (isCacheEnabled()) { templateCache.clear(); if (sourceCache != null) { sourceCache.clear(); } } }
@Override public void renderString(String templateContent, Map<String, Object> model, Writer writer) { String language = (String) model.get(PippoConstants.REQUEST_PARAMETER_LANG); if (StringUtils.isNullOrEmpty(language)) { language = getLanguageOrDefault(language); } // prepare the locale Locale locale = (Locale) model.get(PippoConstants.REQUEST_PARAMETER_LOCALE); if (locale == null) { locale = getLocaleOrDefault(language); } try { localeSupport.setCurrentLocale(locale); Mustache template = engine.compileMustache("StringTemplate", templateContent); template.render(writer, model); writer.flush(); } catch (Exception e) { if (e instanceof PippoRuntimeException) { throw (PippoRuntimeException) e; } throw new PippoRuntimeException(e); } finally { localeSupport.remove(); } }
/** * Don't use the ServiceLoader mechanism to load configuration extensions * (i.e. the default resolvers are not added automatically). * * @see ConfigurationExtension */ public MustacheEngineBuilder omitServiceLoaderConfigurationExtensions() { checkNotBuilt(); this.omitServiceLoaderConfigurationExtensions = true; return this; }
/** * * @param type * @return <code>true</code> if the tag can have a helper associated, * <code>false</code> otherwise */ public static boolean supportsHelpers(MustacheTagType type) { return VARIABLE.equals(type) || UNESCAPE_VARIABLE.equals(type) || SECTION.equals(type) || INVERTED_SECTION.equals(type); }
/** * * @return a new instance of builder */ public static MustacheEngineBuilder newBuilder() { return new MustacheEngineBuilder(); }
/** * * @param options * @return <code>true</code> if the underlying tag represents a variable * which shouldn't be escaped, <code>false</code> otherwise */ public static boolean isUnescapeVariable(Options options) { return options.getTagInfo().getType() .equals(MustacheTagType.UNESCAPE_VARIABLE); }
/** * Sets a locale support instance. * * @param localeSupport * @return self */ public MustacheEngineBuilder setLocaleSupport(LocaleSupport localeSupport) { checkArgumentNotNull(localeSupport); checkNotBuilt(); this.localeSupport = localeSupport; return this; }
public static boolean contentMustBeNonWhitespaceCharacterSequence( MustacheTagType type) { return VARIABLE.equals(type) || UNESCAPE_VARIABLE.equals(type) || SECTION.equals(type) || INVERTED_SECTION.equals(type) || SECTION_END.equals(type) || PARTIAL.equals(type) || EXTEND.equals(type) || EXTEND_SECTION.equals(type) || NESTED_TEMPLATE.equals(type); }
/** * * @param missingValueHandler * @return self */ public MustacheEngineBuilder setMissingValueHandler( MissingValueHandler missingValueHandler) { checkArgumentNotNull(missingValueHandler); checkNotBuilt(); this.missingValueHandler = missingValueHandler; return this; }