本文整理汇总了Java中com.github.jknack.handlebars.io.TemplateSource类的典型用法代码示例。如果您正苦于以下问题:Java TemplateSource类的具体用法?Java TemplateSource怎么用?Java TemplateSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TemplateSource类属于com.github.jknack.handlebars.io包,在下文中一共展示了TemplateSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sourceAt
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public TemplateSource sourceAt(final String location)throws IOException {
String resolvedLocation = this.resolve(location);
String viewPath = view.getPath();
String pathWithoutRenderScript = viewPath.substring(0, viewPath.lastIndexOf(FILE_SEPARATOR));
Bundle bundle = JahiaUtils.getBundle(view);
// This will look partials inside a folder with the same name as the Base Script to use, if you pass a path "/" it will ignore it and use the provided path
String pathToLook = (resolvedLocation.contains("/"))? pathWithoutRenderScript + FILE_SEPARATOR + resolvedLocation : pathWithoutRenderScript + FILE_SEPARATOR + view.getKey() + FILE_SEPARATOR + resolvedLocation;
pathToLook = JahiaUtils.removeBundleSymbolicNameFromModule(bundle.getSymbolicName(), pathToLook);
InputStream is = JahiaUtils.getInputStream(bundle, pathToLook);
if (is == null){
LOG.error("DANTA | Not able to load Partial: --> "+ pathToLook);
}
return new HTMLFileTemplateSource(resource, is);
}
示例2: createFragmentRenderable
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public FragmentRenderableData createFragmentRenderable(FragmentReference fragmentReference, ClassLoader classLoader)
throws RenderableCreationException {
FileReference file = fragmentReference.getRenderingFile();
TemplateSource templateSource = createTemplateSource(file);
Executable executable = createExecutable(fragmentReference, classLoader);
Renderable fragmentRenderable;
if (isDevmodeEnabled) {
MutableHbsFragmentRenderable mfr = new MutableHbsFragmentRenderable(templateSource,
file.getAbsolutePath(),
file.getRelativePath(),
(MutableExecutable) executable);
fragmentRenderable = mfr;
updater.add(fragmentReference, mfr);
} else {
fragmentRenderable = new HbsFragmentRenderable(templateSource, file.getAbsolutePath(),
file.getRelativePath(), executable);
}
Permission permission = new HbsPreprocessor(templateSource).getPermission();
return new RenderableCreator.FragmentRenderableData(fragmentRenderable, permission);
}
示例3: createPageRenderable
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public PageRenderableData createPageRenderable(PageReference pageReference, ClassLoader classLoader)
throws RenderableCreationException {
FileReference file = pageReference.getRenderingFile();
TemplateSource templateSource = createTemplateSource(file);
Executable executable = createExecutable(pageReference, classLoader);
Renderable pageRenderable;
if (isDevmodeEnabled) {
MutableHbsPageRenderable mpr = new MutableHbsPageRenderable(templateSource, file.getAbsolutePath(),
file.getRelativePath(),
(MutableExecutable) executable);
pageRenderable = mpr;
updater.add(pageReference, mpr);
} else {
pageRenderable = new HbsPageRenderable(templateSource, file.getAbsolutePath(), file.getRelativePath(),
executable);
}
HbsPreprocessor preprocessor = new HbsPreprocessor(templateSource);
String layoutName = preprocessor.getLayoutName().orElse(null);
Permission permission = preprocessor.getPermission();
return new RenderableCreator.PageRenderableData(pageRenderable, permission, layoutName);
}
示例4: createLayoutRenderable
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public LayoutRenderableData createLayoutRenderable(LayoutReference layoutReference)
throws RenderableCreationException {
FileReference file = layoutReference.getRenderingFile();
TemplateSource templateSource = createTemplateSource(file);
Renderable layoutRenderable;
if (isDevmodeEnabled) {
MutableHbsLayoutRenderable mlr = new MutableHbsLayoutRenderable(templateSource, file.getAbsolutePath(),
file.getRelativePath());
updater.add(layoutReference, mlr);
layoutRenderable = mlr;
} else {
layoutRenderable = new HbsLayoutRenderable(templateSource, file.getAbsolutePath(), file.getRelativePath());
}
return new RenderableCreator.LayoutRenderableData(layoutRenderable);
}
示例5: render
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public void render(final View view, final Renderer.Context ctx) throws Exception {
String vname = view.name();
TemplateSource source = handlebars.getLoader().sourceAt(vname);
Template template = handlebars.compile(source);
Map<String, Object> locals = ctx.locals();
locals.putIfAbsent("_vname", vname);
locals.putIfAbsent("_vpath", source.filename());
// Locale:
Locale locale = (Locale) locals.getOrDefault("locale", ctx.locale());
locals.put("locale", locale);
com.github.jknack.handlebars.Context context = com.github.jknack.handlebars.Context
.newBuilder(view.model())
// merge request locals (req+sessions locals)
.combine(locals)
.resolver(resolvers)
.build();
// rendering it
ctx.type(MediaType.html)
.send(template.apply(context));
}
示例6: sourceAt
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public TemplateSource sourceAt(final String location)
throws IOException {
Resource scriptResource;
if (location.startsWith("/")) {
ResourceResolver resourceResolver = resource.getResourceResolver();
scriptResource = resourceResolver.getResource(ResourceUtil.normalize(location));
} else {
scriptResource = component.getLocalResource(resolve(location));
}
if (scriptResource == null || ResourceUtil.isNonExistingResource(scriptResource))
throw new IOException("Unable to resolve " + location + " to a valid Resource path.");
else
return new HTMLFileTemplateSource(scriptResource);
}
示例7: MaTemplateEngine
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
public MaTemplateEngine(TemplateLoader loader) {
loader.setSuffix(null);
handlebars = new Handlebars(loader);
// Set Guava cache.
Cache<TemplateSource, Template> cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES)
.maximumSize(1000).build();
handlebars = handlebars.with(new GuavaTemplateCache(cache));
}
示例8: sourceAt
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public TemplateSource sourceAt(final String uri) throws FileNotFoundException {
String location = resolve(normalize(uri));
String text = map.get(location);
if (text == null) {
throw new FileNotFoundException(location);
}
return new StringTemplateSource(location, text);
}
示例9: get
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public Template get(final TemplateSource source, final Parser parser) throws IOException {
notNull(source, "The source is required.");
notNull(parser, "The parser is required.");
/**
* Don't keep duplicated entries, remove old one if a change is detected.
*/
return cacheGet(source, parser);
}
示例10: cacheGet
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
/**
* Get/Parse a template source.
*
* @param source The template source.
* @param parser The parser.
* @return A Handlebars template.
* @throws IOException If we can't read input.
*/
private Template cacheGet(final TemplateSource source, final Parser parser) throws IOException {
Pair<TemplateSource, Template> entry = cache.get(source);
if (entry == null) {
log.debug("Loading: {}", source);
entry = Pair.of(source, parser.parse(source));
cache.put(source, entry);
} else {
log.debug("Found in cache: {}", source);
}
return entry.getValue();
}
示例11: HandlebarsTemplateEngine
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
/**
* Constructs a handlebars template engine
*
* @param resourceRoot the resource root
*/
public HandlebarsTemplateEngine(String resourceRoot) {
TemplateLoader templateLoader = new ClassPathTemplateLoader();
templateLoader.setPrefix(resourceRoot);
templateLoader.setSuffix(null);
handlebars = new Handlebars(templateLoader);
// Set Guava cache.
Cache<TemplateSource, Template> cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES)
.maximumSize(1000).build();
handlebars = handlebars.with(new GuavaTemplateCache(cache));
}
示例12: sourceAt
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public TemplateSource sourceAt(String location) throws IOException {
String loc = resolve(location);
String templ = Utils.readFileToString(vertx, loc);
if (templ == null) {
throw new IllegalArgumentException("Cannot find resource " + loc);
}
long lastMod = System.currentTimeMillis();
return new TemplateSource() {
@Override
public String content() throws IOException {
// load from the file system
return templ;
}
@Override
public String filename() {
return loc;
}
@Override
public long lastModified() {
return lastMod;
}
};
}
示例13: sourceAt
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public TemplateSource sourceAt(String location) throws IOException {
notNull(location, "location must not be null");
Reader reader = loader.getTemplate(location);
String content = read(reader);
return new StringTemplateSource(location, content);
}
示例14: it_should_read_template_source
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Test
public void it_should_read_template_source() throws IOException {
String location = "/templates/foo.template.html";
TemplateSource source = handlebarsTemplateLoader.sourceAt(location);
String expectedContent = "<div>Hello {{name}}</div>";
assertThat(source).isNotNull();
assertThat(source.filename()).isNotNull().isNotEmpty().isEqualTo(location);
assertThat(source.lastModified()).isNotNull().isEqualTo(expectedContent.hashCode());
assertThat(source.content()).isNotNull().isNotEmpty().isEqualTo(expectedContent);
}
示例15: sourceAt
import com.github.jknack.handlebars.io.TemplateSource; //导入依赖的package包/类
@Override
public TemplateSource sourceAt(String location) throws IOException {
String loc = adjustLocation(location);
logger.debug("Loading template from: {}", loc);
AtomicReference<String> templ = new AtomicReference<>();
try {
// find the template on the classpath
Buffer buffer = Utils.readResourceToBuffer(loc);
if (buffer != null) {
templ.set(buffer.toString());
}
} catch (Exception ex) {
logger.warn(
format("Cannot find template: %s on classpath due to: %s!", loc, ex.getMessage()), ex);
}
if (templ.get() == null) {
throw new IllegalArgumentException("Cannot find resource " + loc);
}
long lastMod = System.currentTimeMillis();
return new TemplateSource() {
@Override
public String content() throws IOException {
return templ.get();
}
@Override
public String filename() {
return loc;
}
@Override
public long lastModified() {
return lastMod;
}
};
}