com.github.mustachejava.TemplateContext Java Examples
The following examples show how to use
com.github.mustachejava.TemplateContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MustacheHelper.java From helidon-build-tools with Apache License 2.0 | 6 votes |
@Override public MustacheVisitor createMustacheVisitor() { return new DefaultMustacheVisitor(this) { @Override public void value(TemplateContext tc, String variable, boolean encoded) { list.add(new ValueCode(tc, df, variable, encoded) { @Override public Writer execute(Writer writer, List<Object> scopes) { try { final Object object = get(scopes); if (object instanceof NotEncoded) { writer.write(oh.stringify(object)); return appendText(run(writer, scopes)); } else { return super.execute(writer, scopes); } } catch (Exception e) { throw new MustacheException("Failed to get value for " + name, e, tc); } } }); } }; }
Example #2
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 6 votes |
/** * At compile time, this function extracts the name of the variable: * {{#toJson}}variable_name{{/toJson}} */ protected static String extractVariableName(String fn, Mustache mustache, TemplateContext tc) { Code[] codes = mustache.getCodes(); if (codes == null || codes.length != 1) { throw new MustacheException("Mustache function [" + fn + "] must contain one and only one identifier"); } try (StringWriter capture = new StringWriter()) { // Variable name is in plain text and has type WriteCode if (codes[0] instanceof WriteCode) { codes[0].execute(capture, Collections.emptyList()); return capture.toString(); } else { codes[0].identity(capture); return capture.toString(); } } catch (IOException e) { throw new MustacheException("Exception while parsing mustache function [" + fn + "] at line " + tc.line(), e); } }
Example #3
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
@Override public void iterable(TemplateContext templateContext, String variable, Mustache mustache) { if (ToJsonCode.match(variable)) { list.add(new ToJsonCode(templateContext, df, mustache, variable)); } else if (JoinerCode.match(variable)) { list.add(new JoinerCode(templateContext, df, mustache)); } else if (CustomJoinerCode.match(variable)) { list.add(new CustomJoinerCode(templateContext, df, mustache, variable)); } else if (UrlEncoderCode.match(variable)) { list.add(new UrlEncoderCode(templateContext, df, mustache, variable)); } else { list.add(new IterableCode(templateContext, df, mustache, variable)); } }
Example #4
Source File: TemplateUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
/** * Renders a given Mustache template using the provided value map, returning any template parameters which weren't * present in the map. * * @param templateName descriptive name of template to show in logs * @param templateContent String representation of template * @param values Map of values to be inserted into the template * @param missingValues List where missing value entries will be added for any template params in * {@code templateContent} which are not found in {@code values} * @return Rendered Mustache template String */ public static String renderMustache( String templateName, String templateContent, Map<String, String> values, final List<MissingValue> missingValues) { StringWriter writer = new StringWriter(); DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory(); mustacheFactory.setObjectHandler(new ReflectionObjectHandler() { @Override public Binding createBinding(String name, final TemplateContext tc, Code code) { return new MissingValueBinding(this, name, tc, code, missingValues); } }); Map<String, Object> objEnv = new HashMap<>(); for (Map.Entry<String, String> entry : values.entrySet()) { if (StringUtils.equalsIgnoreCase(entry.getValue(), "false") || StringUtils.equalsIgnoreCase(entry.getValue(), "true")) { objEnv.put(entry.getKey(), Boolean.valueOf(entry.getValue())); } else { objEnv.put(entry.getKey(), entry.getValue()); } } mustacheFactory .compile(new StringReader(templateContent), templateName) .execute(writer, objEnv); return writer.toString(); }
Example #5
Source File: TemplateUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
private MissingValueBinding( ObjectHandler oh, String name, final TemplateContext tc, Code code, List<MissingValue> missingValues) { super(oh, name, tc, code); this.tc = tc; this.code = code; this.missingValues = missingValues; }
Example #6
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 4 votes |
CustomCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String code) { super(tc, df, mustache, extractVariableName(code, mustache, tc)); this.code = Objects.requireNonNull(code); }
Example #7
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 4 votes |
ToJsonCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String variable) { super(tc, df, mustache, CODE); if (!CODE.equalsIgnoreCase(variable)) { throw new MustacheException("Mismatch function code [" + CODE + "] cannot be applied to [" + variable + "]"); } }
Example #8
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 4 votes |
JoinerCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String delimiter) { super(tc, df, mustache, CODE); this.delimiter = delimiter; }
Example #9
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 4 votes |
JoinerCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache) { this(tc, df, mustache, DEFAULT_DELIMITER); }
Example #10
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 4 votes |
CustomJoinerCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String variable) { super(tc, df, mustache, extractDelimiter(variable)); }
Example #11
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 4 votes |
UrlEncoderCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String variable) { super(tc, df, mustache.getCodes(), variable); this.encoder = new UrlEncoder(); }
Example #12
Source File: StrictMustacheFactory.java From spoofax with Apache License 2.0 | 4 votes |
@Override public void value(TemplateContext tc, String variable, boolean encoded) { list.add(new StrictValueCode(tc, df, variable, encoded)); }
Example #13
Source File: StrictMustacheFactory.java From spoofax with Apache License 2.0 | 4 votes |
public StrictValueCode(TemplateContext tc, DefaultMustacheFactory df, String variable, boolean encoded) { super(tc, df, variable, encoded); }