com.samskivert.mustache.Template类的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(154)

本文整理了Java中com.samskivert.mustache.Template类的一些代码示例,展示了Template类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Template类的具体详情如下:
包路径:com.samskivert.mustache.Template
类名称:Template

Template介绍

[英]Represents a compiled template. Templates are executed with a context to generate output. The context can be any tree of objects. Variables are resolved against the context. Given a name foo, the following mechanisms are supported for resolving its value (and are sought in this order):

  • If the variable has the special name this the context object itself will be returned. This is useful when iterating over lists.
  • If the object is a Map, Map#get will be called with the string fooas the key.
  • A method named foo in the supplied object (with non-void return value).
  • A method named getFoo in the supplied object (with non-void return value).
  • A field named foo in the supplied object.

The field type, method return type, or map value type should correspond to the desired behavior if the resolved name corresponds to a section. Boolean is used for showing or hiding sections without binding a sub-context. Arrays, Iterator and Iterableimplementations are used for sections that repeat, with the context bound to the elements of the array, iterator or iterable. Lambdas are current unsupported, though they would be easy enough to add if desire exists. See the Mustache documentation for more details on section behavior.
[中]表示已编译的模板。使用上下文执行模板以生成输出。上下文可以是任何对象树。根据上下文解析变量。如果命名为foo,则支持以下机制来解析其值(并按以下顺序查找):
*如果变量具有特殊名称,则返回上下文对象本身。这在迭代列表时很有用。
*如果对象是Map,则将使用字符串foo作为键调用Map#get。
*提供的对象中名为foo的方法(具有非void返回值)。
*提供的对象中名为getFoo的方法(具有非void返回值)。
*提供的对象中名为foo的字段。
如果解析的名称对应于节,则字段类型、方法返回类型或映射值类型应对应于所需的行为。布尔值用于显示或隐藏节,而不绑定子上下文。数组、迭代器和IterableImplementation用于重复的部分,上下文绑定到数组、迭代器或iterable的元素。Lambda目前不受支持,但如果存在欲望,就很容易添加。有关节行为的更多详细信息,请参见{$0$}。

代码示例

代码示例来源:origin: rapidoid/rapidoid

protected View view(final Template mustache) {
    return (model, out) -> {
      PrintWriter writer = new PrintWriter(out);
      mustache.execute(model, writer);
      writer.flush();
    };
  }
}

代码示例来源:origin: samskivert/jmustache

/**
 * Decomposes the compound key {@code name} into components and resolves the value they
 * reference.
 */
protected Object getCompoundValue (Context ctx, String name, int line, boolean missingIsNull) {
  String[] comps = name.split("\\.");
  // we want to allow the first component of a compound key to be located in a parent
  // context, but once we're selecting sub-components, they must only be resolved in the
  // object that represents that component
  Object data = getValue(ctx, comps[0], line, missingIsNull);
  for (int ii = 1; ii < comps.length; ii++) {
    if (data == NO_FETCHER_FOUND) {
      if (!missingIsNull) throw new MustacheException.Context(
        "Missing context for compound variable '" + name + "' on line " + line +
        ". '" + comps[ii - 1] + "' was not found.", name, line);
      return null;
    } else if (data == null) {
      return null;
    }
    // once we step into a composite key, we drop the ability to query our parent contexts;
    // that would be weird and confusing
    data = getValueIn(data, comps[ii], line);
  }
  return checkForMissing(name, line, missingIsNull, data);
}

代码示例来源:origin: samskivert/jmustache

Object value = getValueIn(ctx.data, name, line);
  return checkForMissing(name, line, missingIsNull, value);
  Object value = getValueIn(pctx.data, name, line);
  if (value != NO_FETCHER_FOUND) return value;
  return getCompoundValue(ctx, name, line, missingIsNull);
} else {
  return checkForMissing(name, line, missingIsNull, NO_FETCHER_FOUND);

代码示例来源:origin: samskivert/jmustache

/**
 * Compiles the supplied template into a repeatedly executable intermediate form.
 */
protected static Template compile (Reader source, Compiler compiler) {
  Accumulator accum = new Parser(compiler).parse(source);
  return new Template(trim(accum.finish(), true), compiler);
}

代码示例来源:origin: com.samskivert/jmustache

Object value = getValueIn(ctx.data, name, line);
  return checkForMissing(name, line, missingIsNull, value);
  Object value = getValueIn(pctx.data, name, line);
  if (value != NO_FETCHER_FOUND) return value;
  return getCompoundValue(ctx, name, line, missingIsNull);
} else {
  return checkForMissing(name, line, missingIsNull, NO_FETCHER_FOUND);

代码示例来源:origin: com.samskivert/jmustache

/**
 * Compiles the supplied template into a repeatedly executable intermediate form.
 */
protected static Template compile (Reader source, Compiler compiler) {
  Accumulator accum = new Parser(compiler).parse(source);
  return new Template(trim(accum.finish(), true), compiler);
}

代码示例来源:origin: spring-io/initializr

public String process(String name, Map<String, ?> model) {
  try {
    Template template = getTemplate(name);
    return template.execute(model);
  }
  catch (Exception ex) {
    log.error("Cannot render: " + name, ex);
    throw new IllegalStateException("Cannot render template", ex);
  }
}

代码示例来源:origin: com.samskivert/jmustache

/**
 * Decomposes the compound key {@code name} into components and resolves the value they
 * reference.
 */
protected Object getCompoundValue (Context ctx, String name, int line, boolean missingIsNull) {
  String[] comps = name.split("\\.");
  // we want to allow the first component of a compound key to be located in a parent
  // context, but once we're selecting sub-components, they must only be resolved in the
  // object that represents that component
  Object data = getValue(ctx, comps[0].intern(), line, missingIsNull);
  for (int ii = 1; ii < comps.length; ii++) {
    if (data == NO_FETCHER_FOUND) {
      if (!missingIsNull) throw new MustacheException.Context(
        "Missing context for compound variable '" + name + "' on line " + line +
        ". '" + comps[ii - 1] + "' was not found.", name, line);
      return null;
    } else if (data == null) {
      return null;
    }
    // once we step into a composite key, we drop the ability to query our parent contexts;
    // that would be weird and confusing
    data = getValueIn(data, comps[ii].intern(), line);
  }
  return checkForMissing(name, line, missingIsNull, data);
}

代码示例来源:origin: killbill/killbill

@Override
  public String executeTemplateText(final String templateText, final Map<String, Object> data) {
    final Template template = Mustache.compiler().nullValue("").compile(templateText);
    return template.execute(data);
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

@Override
protected void renderMergedTemplateModel(Map<String, Object> model,
    HttpServletRequest request, HttpServletResponse response) throws Exception {
  Template template = createTemplate(
      getApplicationContext().getResource(this.getUrl()));
  if (template != null) {
    template.execute(model, response.getWriter());
  }
}

代码示例来源:origin: commonsguy/cw-omnibus

private void printReport() {
 Template tmpl=
   Mustache.compiler().compile(getString(R.string.report_body));
 WebView print=prepPrintWebView(getString(R.string.tps_report));
 print.loadData(tmpl.execute(new TpsReportContext(prose.getText()
                            .toString())),
         "text/html; charset=UTF-8", null);
}

代码示例来源:origin: org.springframework.boot/spring-boot

@Override
protected Mono<Void> renderInternal(Map<String, Object> model, MediaType contentType,
    ServerWebExchange exchange) {
  Resource resource = resolveResource();
  if (resource == null) {
    return Mono.error(new IllegalStateException(
        "Could not find Mustache template with URL [" + getUrl() + "]"));
  }
  DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer();
  try (Reader reader = getReader(resource)) {
    Template template = this.compiler.compile(reader);
    Charset charset = getCharset(contentType).orElse(getDefaultCharset());
    try (Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(),
        charset)) {
      template.execute(model, writer);
      writer.flush();
    }
  }
  catch (Exception ex) {
    DataBufferUtils.release(dataBuffer);
    return Mono.error(ex);
  }
  return exchange.getResponse().writeWith(Flux.just(dataBuffer));
}

代码示例来源:origin: samskivert/jmustache

/**
 * Executes this template with the given context, returning the results as a string.
 * @throws MustacheException if an error occurs while executing or writing the template.
 */
public String execute (Object context) throws MustacheException {
  StringWriter out = new StringWriter();
  execute(context, out);
  return out.toString();
}

代码示例来源:origin: restx/restx

public static String execute(Template mustache, Object scope) {
  return mustache.execute(scope);
}

代码示例来源:origin: com.github.sps.mustache/mustache-spring-view

public void execute(Object context, Object parentContext, Writer out) throws MustacheTemplateException {
    template.execute(context, parentContext, out);
  }
}

代码示例来源:origin: sps/mustache-spring-view

public void execute(Object context, Object parentContext, Writer out) throws MustacheTemplateException {
    template.execute(context, parentContext, out);
  }
}

代码示例来源:origin: io.restx/restx-common

public static String execute(Template mustache, Object scope) {
  return mustache.execute(scope);
}

代码示例来源:origin: com.github.sps.mustache/mustache-spring-view

public void execute(Object context, Writer out) throws MustacheTemplateException {
  template.execute(context, out);
}

代码示例来源:origin: sps/mustache-spring-view

public void execute(Object context, Writer out) throws MustacheTemplateException {
  template.execute(context, out);
}

代码示例来源:origin: com.samskivert/jmustache

/**
 * Executes this template with the given context, returning the results as a string.
 * @throws MustacheException if an error occurs while executing or writing the template.
 */
public String execute (Object context) throws MustacheException {
  StringWriter out = new StringWriter();
  execute(context, out);
  return out.toString();
}

相关文章

微信公众号

最新文章

更多

Template类方法