use of org.apache.velocity.Template in project gocd by gocd.
the class GoVelocityView method getContentAsString.
public String getContentAsString() {
try {
Template template = getTemplate();
StringWriter writer = new StringWriter();
template.merge(null, writer);
return writer.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.apache.velocity.Template in project ngAndroid by davityle.
the class SourceCreator method createSourceFiles.
public void createSourceFiles() {
Properties props = new Properties();
props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SystemLogChute");
props.setProperty("resource.loader", "classpath");
props.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
VelocityEngine ve = new VelocityEngine(props);
ve.init();
Template vtModel = ve.getTemplate("templates/ngmodel.vm");
Template vtScope = ve.getTemplate("templates/scope.vm");
Template vtLayout = ve.getTemplate("templates/layout.vm");
for (NgModelSourceLink ms : modelSourceLinks) {
try {
JavaFileObject jfo = filer.createSourceFile(ms.getSourceFileName(), ms.getElements());
Writer writer = jfo.openWriter();
vtModel.merge(ms.getVelocityContext(), writer);
writer.flush();
writer.close();
} catch (IOException e) {
messageUtils.error(Option.of(ms.getElements()[0]), e.getMessage());
}
}
for (ScopeSourceLink ss : scopeSourceLinks) {
try {
JavaFileObject jfo = filer.createSourceFile(ss.getSourceFileName(), ss.getElements());
Writer writer = jfo.openWriter();
vtScope.merge(ss.getVelocityContext(), writer);
writer.flush();
writer.close();
} catch (IOException e) {
messageUtils.error(Option.of(ss.getElements()[0]), e.getMessage());
}
}
for (LayoutSourceLink lsl : layoutSourceLinks) {
try {
JavaFileObject jfo = filer.createSourceFile(lsl.getSourceFileName(), lsl.getElements());
Writer writer = jfo.openWriter();
vtLayout.merge(lsl.getVelocityContext(), writer);
writer.flush();
writer.close();
} catch (IOException e) {
messageUtils.error(Option.<Element>absent(), e.getMessage());
}
}
}
use of org.apache.velocity.Template in project scoold by Erudika.
the class VelocityLayoutView method renderScreenContent.
/**
* The resulting context contains any mappings from render, plus screen content.
*/
private void renderScreenContent(Context velocityContext) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("Rendering screen content template [" + getUrl() + "]");
}
StringWriter sw = new StringWriter();
Template screenContentTemplate = getTemplate(getUrl());
screenContentTemplate.merge(velocityContext, sw);
// Put rendered content into Velocity context.
velocityContext.put(this.screenContentKey, sw.toString());
}
use of org.apache.velocity.Template in project openolat by klemens.
the class VelocityTemplateTest method testTemplates.
private void testTemplates(String dir, File file, List<Exception> exs) {
String name = file.getName();
if ("_content".equals(name)) {
File[] templates = file.listFiles();
for (File template : templates) {
String templateName = template.getName();
if (templateName.endsWith(".html")) {
try {
String path = dir + templateName;
StringWriter writer = new StringWriter();
Context context = new VelocityContext();
Template veloTemplate = engine.getTemplate(path);
veloTemplate.merge(context, writer);
count++;
} catch (Exception e) {
exs.add(e);
}
}
}
} else if (file.isDirectory()) {
File[] files = file.listFiles();
for (File child : files) {
String subDir = dir + child.getName() + "/";
testTemplates(subDir, child, exs);
}
}
}
use of org.apache.velocity.Template in project openolat by klemens.
the class VelocityHelper method merge.
/**
* @param template e.g. org/olat/demo/_content/index.html
* @param c the context
* @param theme the theme e.g. "accessibility" or "printing". may be null if the default theme ("") should be taken
* @return String the rendered template
*/
private void merge(String template, Context c, Writer wOut, String theme) {
try {
Template vtemplate = null;
if (isLogDebugEnabled())
logDebug("Merging template::" + template + " for theme::" + theme, null);
if (theme != null) {
// try the theme first, if resource not found exception, fallback to normal resource.
// e.g. try /_accessibility/index.html first, if not found, try /index.html.
// this allows for themes to only provide the delta to the default templates
// todo we could avoid those string operations, if the performance gain is measureable
int latestSlash = template.lastIndexOf('/');
StringBuilder sb = new StringBuilder(template.substring(0, latestSlash));
sb.append("/_").append(theme).append("/").append(template.substring(latestSlash + 1));
String themedTemplatePath = sb.toString();
// check cache
boolean notFound = resourcesNotFound.contains(themedTemplatePath);
if (!notFound) {
// never tried before -> try to load it
if (!ve.resourceExists(themedTemplatePath)) {
// this will happen once for each theme when a resource does not exist in its themed variant but only in the default theme.
if (!Settings.isDebuging()) {
resourcesNotFound.add(themedTemplatePath);
}
// for debugging, allow introduction of themed files without restarting the application
} else {
// template exists -> load it
vtemplate = ve.getTemplate(themedTemplatePath, VelocityModule.getInputEncoding());
}
}
// if not found, fallback to standard
if (vtemplate == null) {
vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
}
} else {
// no theme, load the standard template
vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
}
vtemplate.merge(c, wOut);
} catch (MethodInvocationException me) {
throw new OLATRuntimeException(VelocityHelper.class, "MethodInvocationException occured while merging template: methName:" + me.getMethodName() + ", refName:" + me.getReferenceName(), me.getWrappedThrowable());
} catch (Exception e) {
throw new OLATRuntimeException(VelocityHelper.class, "exception occured while merging template: " + e.getMessage(), e);
}
}
Aggregations