template = engine.getMustache(getLocalizedTemplateName(templateName, locale.toString())); if (template == null) { template = engine.getMustache(getLocalizedTemplateName(templateName, locale.getLanguage())); template = engine.getMustache(templateName);
@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(); } }
/** * * @param engine * @return a new decorator instance * @throws IllegalStateException * If {@link ReflectionResolver} is not available */ public Decorator<T> build(MustacheEngine engine) { return build(engine.getConfiguration()); }
@Override public String source(String templateId) { Checker.checkArgumentNotEmpty(templateId); String mustacheSource = engine.getMustacheSource(templateId); if (mustacheSource == null) { throw new MustacheException( MustacheProblem.RENDER_INVALID_PARTIAL_KEY, "No mustache template found for the given key: %s %s", templateId, segment.getOrigin()); } return mustacheSource; }
private Templates(MustacheEngine engine, Set<String> names) { this.engine = engine; for(String name : names) { if(!name.startsWith("@")) { all.add(engine.getMustache(name)); } } }
@Override public void engineBuilt(MustacheEngine engine) { this.localeSupport = engine.getConfiguration().getLocaleSupport(); }
@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(); } }
/** * * @param templateId * @param engine * @param currentTemplate * @return the template or <code>null</code> */ public static Template lookupTemplate(String templateId, MustacheEngine engine, Template currentTemplate) { Template result = null; if (currentTemplate != null) { result = currentTemplate.getNestedTemplate(templateId); } if (result == null) { result = (Template) engine.getMustache(templateId); } return result; }
private void renderingFinished(MustacheRenderingEvent event) { List<MustacheListener> listeners = engine.getConfiguration() .getMustacheListeners(); if (listeners.isEmpty()) { return; } for (ListIterator<MustacheListener> iterator = listeners .listIterator(listeners.size()); iterator.hasPrevious();) { iterator.previous().renderingFinished(event); } }
private void processLambda(Appendable appendable, ExecutionContext context, Object value) { Lambda lambda = (Lambda) value; String input; switch (lambda.getInputType()) { case LITERAL: // Try to reconstruct the original text input = getContentLiteralBlock(); break; case PROCESSED: StringBuilder processed = new StringBuilder(); super.execute(processed, context); input = processed.toString(); break; default: throw new IllegalStateException("Unsupported lambda input type"); } String returnValue = lambda.invoke(input); if (lambda.isReturnValueInterpolated()) { // Parse and interpolate the return value Template temp = (Template) getEngine().compileMustache( Lambdas.constructLambdaOneoffTemplateName(this), returnValue); temp.getRootSegment().execute(appendable, context); } else { append(appendable, returnValue); } }
private void writeApiInterface( GenerateTaskBuilder genTaskBuilder, Name packageName, String interfaceName) throws IOException { final Map<String, Object> data = new HashMap<>(); data.put("packageName", packageName); data.put("interfaceName", interfaceName); data.put("genFn", IntStream.rangeClosed(0, genTaskBuilder.upTo()) .mapToObj(this::fn).toArray()); data.put("genBuilder", IntStream.range(1, genTaskBuilder.upTo()) .mapToObj(this::builder).toArray()); final String output = engine.getMustache("TaskBuilder").render(data); final String outputScala = engine.getMustache("ScalaApi").render(data); if (!genTaskBuilder.scala()) { final String fileName = packageName + "." + interfaceName; final JavaFileObject filerSourceFile = filer.createSourceFile(fileName, processingElement); try (final Writer writer = filerSourceFile.openWriter()) { writer.write(output); } } else { final FileObject scalaFile = filer.createResource( StandardLocation.SOURCE_OUTPUT, packageName, "ScalaApi.scala", processingElement); try (final Writer writer = scalaFile.openWriter()) { writer.write(outputScala); } } }
DefaultValuePlaceholder(String name, MustacheEngine engine) { this.name = name; this.provider = new ValueProvider(name, engine.getConfiguration()); }
private void processLambda(Appendable appendable, ExecutionContext context, Object value) { Lambda lambda = (Lambda) value; String returnValue = lambda.invoke(null); if (returnValue == null) { Object replacement = getEngineConfiguration() .getMissingValueHandler().handle(getTagInfo()); if (replacement != null) { processValue(appendable, context, replacement); return; } } else if (!returnValue.equals(Strings.EMPTY)) { if (lambda.isReturnValueInterpolated()) { // Parse and interpolate the return value StringBuilder interpolated = new StringBuilder(); Template temp = (Template) getEngine().compileMustache( Lambdas.constructLambdaOneoffTemplateName(this), returnValue); temp.getRootSegment().execute(interpolated, context); writeValue(appendable, interpolated.toString()); } else { writeValue(appendable, returnValue); } } }
data.put("lastArguments", arguments(n)); data.put("lastProcessArgs", processArgs(n)); final String output = engine.getMustache("TaskBuilderImpl").render(data); final String outputScala = engine.getMustache("ScalaApiImpl").render(data);
@Override public void engineBuilt(MustacheEngine engine) { minifier.init(engine.getConfiguration()); }
template = engine.getMustache(getLocalizedTemplateName(templateName, locale.toString())); if (template == null) { template = engine.getMustache(getLocalizedTemplateName(templateName, locale.getLanguage())); template = engine.getMustache(templateName);
protected Configuration getEngineConfiguration() { return getEngine().getConfiguration(); }
@Setup public void setup() { template = MustacheEngineBuilder.newBuilder() // Disable HTML escaping .setProperty(EngineConfigurationKey.SKIP_VALUE_ESCAPING, true) // Disable useless resolver .setProperty(CombinedIndexResolver.ENABLED_KEY, false) .addTemplateLocator(ClassPathTemplateLocator.builder(1).setRootPath("templates").setScanClasspath(false).setSuffix("trimou.html").build()) .registerHelpers(HelpersBuilder.extra().build()) // This is a single purpose helper // It's a pity we can't use JDK8 extension and SimpleHelpers util class .registerHelper("minusClass", new BasicValueHelper() { @Override public void execute(Options options) { Object value = options.getParameters().get(0); if (value instanceof Double && (Double) value < 0) { options.append(" class=\"minus\""); } // We don't handle any other number types } }).build().getMustache("stocks"); this.context = getContext(); }
@Override public void startTemplate(String name, Delimiters delimiters, MustacheEngine engine) { this.delimiters = delimiters; this.engine = engine; this.templateName = name; containerStack.addFirst(new RootSegmentBase()); skipValueEscaping = engine.getConfiguration().getBooleanPropertyValue( EngineConfigurationKey.SKIP_VALUE_ESCAPING); handlebarsSupportEnabled = engine.getConfiguration() .getBooleanPropertyValue( EngineConfigurationKey.HANDLEBARS_SUPPORT_ENABLED); start = System.currentTimeMillis(); LOGGER.debug("Start compilation of {}", new Object[] { name }); }
private void renderingStarted(MustacheRenderingEvent event) { List<MustacheListener> listeners = engine.getConfiguration() .getMustacheListeners(); if (listeners.isEmpty()) { return; } for (MustacheListener listener : listeners) { listener.renderingStarted(event); } }