Search in sources :

Example 41 with Template

use of org.apache.velocity.Template in project lucene-solr by apache.

the class VelocityResponseWriter method getTemplate.

private Template getTemplate(VelocityEngine engine, SolrQueryRequest request) throws IOException {
    Template template;
    String templateName = request.getParams().get(TEMPLATE);
    String qt = request.getParams().get(CommonParams.QT);
    String path = (String) request.getContext().get("path");
    if (templateName == null && path != null) {
        templateName = path;
    }
    // TODO: path is never null, so qt won't get picked up  maybe special case for '/select' to use qt, otherwise use path?
    if (templateName == null && qt != null) {
        templateName = qt;
    }
    if (templateName == null)
        templateName = "index";
    try {
        template = engine.getTemplate(templateName + TEMPLATE_EXTENSION);
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
    return template;
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Example 42 with Template

use of org.apache.velocity.Template in project Gargoyle by callakrsos.

the class MailUtil method getTemplateFromFile.

/**
	 * 파일로부터 템플릿 정보를 얻어온다.
	 *
	 * @Date 2015. 9. 13.
	 * @param templateFileName
	 * @return
	 * @throws Exception
	 * @User KYJ
	 */
public static Template getTemplateFromFile(final String templateFileName) throws Exception {
    String readFileToString = "";
    if (templateFileName.startsWith("classpath:")) {
        String res = templateFileName.replace("classpath:", "");
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream(res);
        readFileToString = ValueUtil.toString(resourceAsStream);
    } else
        readFileToString = FileUtils.readFileToString(new File(templateFileName));
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(readFileToString);
    SimpleNode node = runtimeServices.parse(reader, templateFileName);
    Template template = new Template();
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();
    return template;
}
Also used : RuntimeServices(org.apache.velocity.runtime.RuntimeServices) InputStream(java.io.InputStream) StringReader(java.io.StringReader) File(java.io.File) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode) Template(org.apache.velocity.Template)

Example 43 with Template

use of org.apache.velocity.Template in project stanbol by apache.

the class BenchmarkServlet method getTemplate.

/**
     * @return
     */
private Template getTemplate(String templatePath) {
    final Template t;
    ClassLoader tcl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(BenchmarkServlet.class.getClassLoader());
    try {
        t = velocity.getTemplate(templatePath);
    } finally {
        Thread.currentThread().setContextClassLoader(tcl);
    }
    return t;
}
Also used : Template(org.apache.velocity.Template)

Example 44 with Template

use of org.apache.velocity.Template in project gocd by gocd.

the class VelocityTemplateEngine method render.

@Override
public String render(ModelAndView modelAndView) {
    Template template = velocityEngine.getTemplate(layout, "utf-8");
    Object model = modelAndView.getModel();
    if (model == null) {
        model = Collections.emptyMap();
    }
    if (model instanceof Map) {
        VelocityContext context = initialContextProvider.getVelocityContext((Map) model, controller, modelAndView.getViewName());
        StringWriter writer = new StringWriter();
        template.merge(context, writer);
        return writer.toString();
    } else {
        throw new IllegalArgumentException("modelAndView must be of type java.util.Map");
    }
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Map(java.util.Map) Template(org.apache.velocity.Template)

Example 45 with Template

use of org.apache.velocity.Template in project gocd by gocd.

the class TestVelocityView method getTemplate.

@Override
protected Template getTemplate() throws Exception {
    Template realTemplateForTest = setupTemplate(loader, runtimeServices, "template1", getClass().getResourceAsStream(templatePath));
    List<Template> templates = new ArrayList<>();
    templates.add(realTemplateForTest);
    templates.addAll(additionalTemplates);
    for (Template template : templates) {
        template.process();
    }
    return templates.get(0);
}
Also used : ArrayList(java.util.ArrayList) Template(org.apache.velocity.Template)

Aggregations

Template (org.apache.velocity.Template)59 VelocityContext (org.apache.velocity.VelocityContext)41 StringWriter (java.io.StringWriter)28 VelocityEngine (org.apache.velocity.app.VelocityEngine)21 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)12 IOException (java.io.IOException)11 ParseErrorException (org.apache.velocity.exception.ParseErrorException)8 Writer (java.io.Writer)7 File (java.io.File)6 Map (java.util.Map)6 Properties (java.util.Properties)6 FileWriter (java.io.FileWriter)5 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)5 PrintWriter (java.io.PrintWriter)4 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 FileOutputStream (java.io.FileOutputStream)3 HashMap (java.util.HashMap)3 UIContext (com.github.bordertech.wcomponents.UIContext)2