本文整理汇总了Java中com.google.template.soy.tofu.SoyTofu类的典型用法代码示例。如果您正苦于以下问题:Java SoyTofu类的具体用法?Java SoyTofu怎么用?Java SoyTofu使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SoyTofu类属于com.google.template.soy.tofu包,在下文中一共展示了SoyTofu类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createTofuSupplier
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
/** Returns a memoized supplier containing compiled tofu. */
public static Supplier<SoyTofu> createTofuSupplier(final SoyFileInfo... soyInfos) {
return memoize(
() -> {
ConsoleDebug debugMode = ConsoleDebug.get();
SoyFileSet.Builder builder = SoyFileSet.builder();
for (SoyFileInfo soyInfo : soyInfos) {
builder.add(getResource(soyInfo.getClass(), soyInfo.getFileName()));
}
Map<String, Object> globals = new HashMap<>();
try {
globals.putAll(SoyUtils.parseCompileTimeGlobals(asCharSource(SOY_GLOBALS, UTF_8)));
} catch (SoySyntaxException | IOException e) {
throw new RuntimeException("Failed to load soy globals", e);
}
globals.put("DEBUG", debugMode.ordinal());
builder.setCompileTimeGlobals(globals);
return builder.build().compileToTofu();
});
}
示例2: compiledTemplates
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
public Optional<SoyTofu> compiledTemplates() throws IOException {
if (shouldCompileTemplates()) {
return Locks.withLock(cacheLock, () -> { //loan pattern
Optional<SoyTofu> soyTofuOpt = Optional.ofNullable(cache.getIfPresent(TEMPLATES_KEY));
if (soyTofuOpt.isPresent()) {
return soyTofuOpt;
}
final Stopwatch stopwatch = Stopwatch.createStarted();
logger.debug("Compiling templates...");
final SoyTofu soyTofu = compileTemplates();
cache.put(TEMPLATES_KEY, soyTofu);
stopwatch.stop();
logger.info(String.format("Compiling templates, took: %d ms", stopwatch.elapsed(TimeUnit.MILLISECONDS)));
return Optional.of(soyTofu);
});
}
return Optional.ofNullable(cache.getIfPresent(TEMPLATES_KEY));
}
示例3: setupRenderer
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
protected void setupRenderer(final SoyTofu.Renderer renderer, final RenderRequest renderRequest, final Optional<SoyMapData> model) {
if (model.isPresent()) {
logger.debug("renderer - model is available.");
renderer.setData(model.get());
}
final Optional<SoyMapData> globalModel = renderRequest.getGlobalRuntimeModel();
if (globalModel.isPresent()) {
logger.debug("renderer - global runtime model is available.");
renderer.setIjData(globalModel.get());
}
final Optional<SoyMsgBundle> soyMsgBundleOpt = renderRequest.getSoyMsgBundle();
if (soyMsgBundleOpt.isPresent()) {
logger.debug("renderer - soyMsgBundle is available.");
renderer.setMsgBundle(soyMsgBundleOpt.get());
if (!soyViewConf.globalHotReloadMode()) {
if (renderRequest.getCompiledTemplates().isPresent()) {
renderRequest.getCompiledTemplates().get().addToCache(soyMsgBundleOpt.get(), null);
}
}
}
if (soyViewConf.globalHotReloadMode()) {
renderer.setDontAddToCache(true);
}
}
示例4: createAndMaybeCacheSoyTofu
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
/**
* Returns the {@link SoyTofu} for {@link TemplateHandlerDelegate#getTemplates()}. If
* {@link #DEBUG} is {@code false}, then the result will be cached because {@link SoyTofu} objects
* can be expensive to construct.
*/
private SoyTofu createAndMaybeCacheSoyTofu() {
// In debug mode, create a new SoyTofu object for each request. This makes it possible to test
// new versions of the templates without restarting buckd.
if (DEBUG) {
return createSoyTofu();
}
// In production, cache the SoyTofu object for efficiency.
if (tofu != null) {
return tofu;
}
synchronized (this) {
if (tofu == null) {
tofu = createSoyTofu();
}
}
return tofu;
}
示例5: render
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
@Override
public void render(final RenderRequest renderRequest) throws Exception {
if (!renderRequest.getCompiledTemplates().isPresent()) {
logger.warn("compiled templates are not present, nothing to render!");
return;
}
final SoyTofu compiledTemplates = renderRequest.getCompiledTemplates().get();
final String templateName = renderRequest.getTemplateName();
final SoyTofu.Renderer renderer = compiledTemplates.newRenderer(templateName);
final Optional<SoyMapData> soyModel = toSoyDataConverter.toSoyMap(renderRequest.getModel());
setupRenderer(renderer, renderRequest, soyModel);
writeResponse(renderer, renderRequest);
}
示例6: setupRenderer
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
protected void setupRenderer(final SoyTofu.Renderer renderer, final RenderRequest renderRequest, final Optional<SoyMapData> model) throws Exception {
if (model.isPresent()) {
renderer.setData(model.get());
}
final Optional<SoyMapData> globalModel = renderRequest.getGlobalRuntimeModel();
if (globalModel.isPresent()) {
renderer.setIjData(globalModel.get());
}
final Optional<SoyMsgBundle> soyMsgBundleOptional = renderRequest.getSoyMsgBundle();
if (soyMsgBundleOptional.isPresent()) {
renderer.setMsgBundle(soyMsgBundleOptional.get());
if (isHotReloadModeOff()) {
if (renderRequest.getCompiledTemplates().isPresent()) {
renderRequest.getCompiledTemplates().get().addToCache(soyMsgBundleOptional.get(), null);
}
}
}
if (isHotReloadMode()) {
renderer.setDontAddToCache(true);
}
}
示例7: initialize
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
public AmzaBotUIService initialize(String cacheToken,
AmzaBotUIServiceConfig config) {
File soyPath = new File(System.getProperty("user.dir"), config.getPathToSoyResources());
SoyFileSet.Builder soyFileSetBuilder = new SoyFileSet.Builder();
FileUtils.listFiles(soyPath, null, true).forEach(soyFileSetBuilder::add);
SoyFileSet sfs = soyFileSetBuilder.build();
SoyTofu tofu = sfs.compileToTofu();
SoyRenderer renderer = new SoyRenderer(tofu, new SoyDataUtils());
AmzaBotRegion contentRegion = new AmzaBotRegion("soy.content.page.main", renderer);
UiHeaderRegion headerRegion = new UiHeaderRegion("soy.ui.chrome.headerRegion", renderer);
return new AmzaBotUIService(
new UiChromeRegion<>(cacheToken, "soy.ui.chrome.chromeRegion", renderer, headerRegion, contentRegion));
}
示例8: IndexServlet
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
IndexServlet(String canonicalURL, @Nullable String cdnPath, @Nullable String faviconPath)
throws URISyntaxException {
String resourcePath = "com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy";
SoyFileSet.Builder builder = SoyFileSet.builder();
builder.add(Resources.getResource(resourcePath));
SoyTofu.Renderer renderer =
builder
.build()
.compileToTofu()
.newRenderer("com.google.gerrit.httpd.raw.Index")
.setContentKind(SanitizedContent.ContentKind.HTML)
.setData(getTemplateData(canonicalURL, cdnPath, faviconPath));
indexSource = renderer.render().getBytes(UTF_8);
}
示例9: get
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
@Override
public SoyTofu get() throws ProvisionException {
SoyFileSet.Builder builder = SoyFileSet.builder();
builder.setSoyAstCache(cache);
for (String name : TEMPLATES) {
addTemplate(builder, name);
}
return builder.build().compileToTofu();
}
示例10: compileTemplates
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
private SoyTofu compileTemplates() throws IOException {
Preconditions.checkNotNull(templatesFileResolver, "templatesRenderer cannot be null!");
Preconditions.checkNotNull(tofuCompiler, "tofuCompiler cannot be null!");
final Collection<URL> templateFiles = templatesFileResolver.resolve();
if (templateFiles.size() > 0) {
logger.debug("Compiling templates, no:{}", templateFiles.size());
return tofuCompiler.compile(templateFiles);
}
throw new IOException("0 template files have been found, check your templateFilesResolver!");
}
示例11: render
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
@Override
public String render(final RenderRequest renderRequest) {
final SoyTofu compiledTemplates = renderRequest.getCompiledTemplates().get();
final String templateName = renderRequest.getTemplateName();
logger.debug("rendering templateName:" + templateName);
final SoyTofu.Renderer renderer = compiledTemplates.newRenderer(templateName);
final Optional<SoyMapData> soyModel = renderRequest.getSoyModel();
setupRenderer(renderer, renderRequest, soyModel);
return renderer.render();
}
示例12: main
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
/**
* Prints the generated HTML to stdout.
*
* @param args Not used.
*/
public static void main(String[] args) {
// Compile the template.
SoyFileSet sfs = SoyFileSet.builder().add(Resources.getResource("simple.soy")).build();
SoyTofu tofu = sfs.compileToTofu();
// Example 1.
writeExampleHeader();
System.out.println(tofu.newRenderer("soy.examples.simple.helloWorld").render());
// Create a namespaced tofu object to make calls more concise.
SoyTofu simpleTofu = tofu.forNamespace("soy.examples.simple");
// Example 2.
writeExampleHeader();
System.out.println(
simpleTofu.newRenderer(".helloName").setData(new SoyMapData("name", "Ana")).render());
// Example 3.
writeExampleHeader();
System.out.println(
simpleTofu
.newRenderer(".helloNames")
.setData(new SoyMapData("names", new SoyListData("Bob", "Cid", "Dee")))
.render());
}
示例13: forNamespace
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
@Override
public SoyTofu forNamespace(@Nullable String namespace) {
if (namespace == null) {
return baseTofu;
} else {
checkArgument(
namespace.charAt(0) != '.' && namespace.charAt(namespace.length() - 1) != '.',
"Invalid namespace '%s' (must not begin or end with a dot).",
namespace);
return new NamespacedTofu(baseTofu, namespace);
}
}
示例14: compileToTofu
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
/**
* Compiles this Soy file set into a Java object (type {@code SoyTofu}) capable of rendering the
* compiled templates.
*
* @return The resulting {@code SoyTofu} object.
* @throws SoyCompilationException If compilation fails.
*/
public SoyTofu compileToTofu() {
resetErrorReporter();
ServerCompilationPrimitives primitives = compileForServerRendering();
throwIfErrorsPresent();
SoyTofu tofu = doCompileToTofu(primitives);
reportWarnings();
return tofu;
}
示例15: doCompileToTofu
import com.google.template.soy.tofu.SoyTofu; //导入依赖的package包/类
/** Helper method to compile SoyTofu from {@link ServerCompilationPrimitives} */
private SoyTofu doCompileToTofu(ServerCompilationPrimitives primitives) {
return new BaseTofu(
soyValueConverter,
apiCallScopeProvider,
primitives.registry,
getTransitiveIjs(primitives.soyTree, primitives.registry));
}