本文整理汇总了Java中com.github.jknack.handlebars.Context类的典型用法代码示例。如果您正苦于以下问题:Java Context类的具体用法?Java Context怎么用?Java Context使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Context类属于com.github.jknack.handlebars包,在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: render
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
@Override
public void render(
RoutingContext context, String templateFileName, Handler<AsyncResult<Buffer>> handler) {
try {
Template template = cache.get(templateFileName);
if (template == null) {
synchronized (this) {
loader.setVertx(context.vertx());
template = handlebars.compile(templateFileName);
cache.put(templateFileName, template);
}
}
Context engineContext = Context.newBuilder(context.data()).resolver(getResolvers()).build();
handler.handle(Future.succeededFuture(Buffer.buffer(template.apply(engineContext))));
} catch (Exception ex) {
handler.handle(Future.failedFuture(ex));
}
}
示例2: compileHandlebarsAndApply
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
private static Builder compileHandlebarsAndApply(final Handlebars handlebars,
final String templateName,
final Map<String, Object> model,
final Function<String, Builder> method) {
Preconditions.checkArgument(StringUtils.hasText(templateName), "Template name not specified");
final Context context = Context.newBuilder(model).combine(model).build();
try {
return method.apply(handlebars.compile(templateName).apply(context));
} catch (IOException e) {
throw new RuntimeException("Could not render template", e);
} finally {
context.destroy();
}
}
示例3: compileWith
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
String compileWith(Handlebars handlebars) {
try {
Template compiledFragment = handlebars.compileInline(unwrappedContent);
LOGGER.trace("Applying context [{}] to fragment [{}]", fragment.context(),
StringUtils
.abbreviate(fragment.content().replaceAll("[\n\r\t]", ""),
MAX_FRAGMENT_CONTENT_LOG_LENGTH));
return compiledFragment.apply(
Context.newBuilder(fragment.context())
.push(JsonObjectValueResolver.INSTANCE)
.build());
} catch (IOException e) {
LOGGER.error("Could not process fragment [{}]", fragment.content(), e);
throw new IllegalStateException("Handlebars fragment can not be evaluated correctly.");
}
}
示例4: render
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
private String render(final boolean isInline, final String source, final Object data, final String languageCode) {
try {
final Template template = isInline ? handlebars.compileInline(source) : handlebars.compile(source);
final Context context = Context.newBuilder(data).combine(LANGUAGE_PROPERTY, languageCode)
.resolver(
ScalaJsonValueResolver.INSTANCE,
JsonNodeValueResolver.INSTANCE,
MapValueResolver.INSTANCE,
FieldValueResolver.INSTANCE)
.build();
return template.apply(context);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例5: serialize
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
@Override
public JsonElement serialize(Context context, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject serialized = jsonSerializationContext.serialize(context.model()).getAsJsonObject();
try {
Field extendedContextField = ((Class) type).getDeclaredField("extendedContext");
extendedContextField.setAccessible(true);
Context extendedContext = (Context) extendedContextField.get(context);
if (extendedContext != null) {
JsonObject extendedContextJson = jsonSerializationContext.serialize(extendedContext.model())
.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entries = extendedContextJson.entrySet();
for (Map.Entry<String, JsonElement> entry : entries) {
serialized.add(entry.getKey(), entry.getValue());
}
}
} catch (NoSuchFieldException | IllegalAccessException e) {
log.error("Error while serializing the handlebars context: " + context);
}
return serialized;
}
示例6: hashContext
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
/**
* Iterate over a hash like object.
*
* @param context The context object.
* @param options The helper options.
* @return The string output.
* @throws IOException If something goes wrong.
*/
private CharSequence hashContext(final Object context, final Options options)
throws IOException {
Set < Entry < String, Object >> propertySet = options
.propertySet(context);
StringBuilder buffer = new StringBuilder();
Context parent = options.context;
boolean first = true;
for (Entry < String, Object > entry : propertySet) {
Context current = Context.newBuilder(parent, entry.getValue())
.combine("@key", entry.getKey())
.combine("@first", first ? "first" : "").build();
buffer.append(options.fn(current));
current.destroy();
first = false;
}
return buffer.toString();
}
示例7: ensureReleaseBranch
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
private void ensureReleaseBranch(AdvancedSCMManager amm, String targetBranch) throws AdvancedSCMException, ReleaseBranchInvalidException{
String releaseFileContent = null;
if (releaseFileContentTemplate != null && !releaseFileContentTemplate.isEmpty()
&& releaseFilePath != null && !releaseFilePath.isEmpty()) {
Handlebars handlebars = new Handlebars();
Context templateContext = Context.newContext(null);
templateContext.data("release", amm.getReleaseBranch(targetBranch).getReleaseName());
Template mustacheTemplate;
try {
mustacheTemplate = handlebars.compileInline(releaseFileContentTemplate);
releaseFileContent = mustacheTemplate.apply(templateContext);
} catch (IOException e) {
throw new AdvancedSCMException("Error rendering release file content template");
}
}
amm.ensureReleaseBranch(
targetBranch, releaseFilePath, releaseFileContent,
"[Jenkins Integration Merge] " + targetBranch + " release", commitUsername);
}
示例8: onRender
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
@Override
protected void onRender(Node content, RenderableDefinition definition, RenderingContext renderingContext,
Map<String, Object> context, String templateScript) throws RenderException {
final AppendableWriter out;
try {
out = renderingContext.getAppendable();
AggregationState aggregationState = (AggregationState) context.get("state");
Node node = aggregationState.getCurrentContentNode();
Locale locale = aggregationState.getLocale();
context.put("content", new ChainedContentMap(node, locale));
Context combinedContext = Context.newBuilder(context)
.resolver(JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE, MapValueResolver.INSTANCE)
.build();
try {
Template template = handlebars.compile(templateScript);
template.apply(combinedContext, out);
} finally {
combinedContext.destroy();
}
} catch (IOException e) {
LOGGER.error("Cannot render template", e);
}
}
示例9: render
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
@Override
public void render(RoutingContext context, String templateDirectory, String templateFileName, Handler<AsyncResult<Buffer>> handler) {
try {
String baseTemplateFileName = templateFileName;
templateFileName = templateDirectory + templateFileName;
Template template = isCachingEnabled() ? cache.get(templateFileName) : null;
if (template == null) {
synchronized (this) {
loader.setPrefix(templateDirectory);
loader.setVertx(context.vertx());
// Strip leading slash from Utils##normalizePath
template = handlebars.compile(baseTemplateFileName.substring(1));
if (isCachingEnabled()) {
cache.put(templateFileName, template);
}
}
}
Context engineContext = Context.newBuilder(context.data()).resolver(getResolvers()).build();
handler.handle(Future.succeededFuture(Buffer.buffer(template.apply(engineContext))));
} catch (Exception ex) {
handler.handle(Future.failedFuture(ex));
}
}
示例10: request
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
protected final SlingHttpServletRequest request() {
Options options = options();
if (options != null) {
Context context = options.context;
SlingHttpServletRequest result = (SlingHttpServletRequest) context.get(SLING_HTTP_REQUEST);
return result;
}
return null;
}
示例11: toJSONObject
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
public JSONObject toJSONObject() {
synchronized (currentScopeData()) {
JSONObject modelDataObj = wrap(currentScopeData());
Context context = currentContext.parent();
while (context != null) {
modelDataObj.merge(context.model());
context = context.parent();
}
return modelDataObj;
}
}
示例12: retractScope
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
public synchronized AbstractTemplateContentModelImpl retractScope() {
if (currentContext != rootContext) {
Context oldContext = currentContext;
currentContext = currentContext.parent();
oldContext.destroy();
}
invalidateJSONString();
return this;
}
示例13: render
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
public Future<String> render(String template, String destination, JsonObject object) {
Context context = Context.newBuilder(object.getMap()).resolver(MapValueResolver.INSTANCE).build();
log.debug("Rendering template {} with object {}", object);
Future future = Future.future();
try {
future.complete(writeFile(destination, handlebars.compile(template).apply(context)));
} catch (IOException e) {
log.error("Impossible to render template {}: ", template, e);
future.fail(e.getCause());
}
return future;
}
示例14: render
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
public Future<String> render(String template, JsonObject object) {
Context context = Context.newBuilder(object.getMap()).resolver(MapValueResolver.INSTANCE).build();
log.debug("Rendering template {} with object {}", object);
Future future = Future.future();
try {
future.complete(handlebars.compile(template).apply(context));
} catch (IOException e) {
log.error("Impossible to render template {}: ", template, e.getMessage());
future.fail(e.getCause());
}
return future;
}
示例15: request
import com.github.jknack.handlebars.Context; //导入依赖的package包/类
protected final HttpServletRequest request() {
Options options = options();
if (options != null) {
Context context = options.context;
HttpServletRequest result = (HttpServletRequest) context.get(HTTP_REQUEST);
return result;
}
return null;
}