Search in sources :

Example 1 with TemplateVariable

use of io.leopard.web.freemarker.TemplateVariable in project leopard by tanhaichao.

the class AbstractTemplateView method render.

public void render(HttpServletRequest request, HttpServletResponse response) throws IOException {
    RequestHolder.setRequest(request);
    Configuration config = new Configuration();
    ConfigurationHolder.setConfiguration(config);
    // 设置要解析的模板所在的目录,并加载模板文件
    // config.setDirectoryForTemplateLoading(templateFile);
    config.setTemplateLoader(new ClassTemplateLoader(this.getClass(), folder));
    // 设置包装器,并将对象包装为数据模型
    config.setObjectWrapper(new DefaultObjectWrapper());
    config.setDefaultEncoding("UTF-8");
    Map<String, Object> freemarkerVariables = new HashMap<String, Object>();
    freemarkerVariables.put("xml_escape", "fmXmlEscape");
    List<TemplateVariable> variables = this.getVariables();
    if (variables != null) {
        for (TemplateVariable variable : variables) {
            freemarkerVariables.put(variable.getKey(), variable);
        }
    }
    // freemarkerVariables.put("time", new TimeTemplateMethod());
    // freemarkerVariables.put("url", new UrlTemplateMethod());
    // freemarkerVariables.put("underline", new UnderlineTemplateMethod());
    Properties freemarkerSettings = new Properties();
    freemarkerSettings.put("template_update_delay", "1");
    freemarkerSettings.put("defaultEncoding", "UTF-8");
    try {
        config.setSettings(freemarkerSettings);
        config.setAllSharedVariables(new SimpleHash(freemarkerVariables, config.getObjectWrapper()));
    } catch (TemplateException e) {
        throw new IOException(e);
    }
    // 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
    // 否则会出现乱码
    Template template = config.getTemplate(templateName + ".ftl", Locale.CHINA, "UTF-8");
    template.setCustomAttribute("request", request);
    model.put("request", request);
    StringWriter writer = new StringWriter();
    // Writer out = response.getWriter();
    try {
        template.process(model, writer);
    } catch (TemplateException e) {
        throw new IOException(e);
    }
    output(writer, response);
// Template tmp = (getEncoding() != null ? conf.getTemplate(name,
// locale, getEncoding()) : conf.getTemplate(name, locale));
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TemplateException(freemarker.template.TemplateException) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) DefaultObjectWrapper(freemarker.template.DefaultObjectWrapper) IOException(java.io.IOException) Properties(java.util.Properties) Template(freemarker.template.Template) StringWriter(java.io.StringWriter) SimpleHash(freemarker.template.SimpleHash) TemplateVariable(io.leopard.web.freemarker.TemplateVariable)

Example 2 with TemplateVariable

use of io.leopard.web.freemarker.TemplateVariable in project leopard by tanhaichao.

the class MyJettyView method getVariables.

@Override
public List<TemplateVariable> getVariables() {
    List<TemplateVariable> list = new ArrayList<TemplateVariable>();
    list.add(new TimeTemplateMethod());
    list.add(new BodyTemplateDirective());
    list.add(new ServerInfoTemplateDirective());
    list.add(new ReplaceParamTemplateMethod());
    list.add(new ButtonAjaxTemplateDirective());
    return list;
}
Also used : ButtonAjaxTemplateDirective(io.leopard.web.freemarker.template.ajax.ButtonAjaxTemplateDirective) ArrayList(java.util.ArrayList) TemplateVariable(io.leopard.web.freemarker.TemplateVariable) TimeTemplateMethod(io.leopard.web.freemarker.template.TimeTemplateMethod) BodyTemplateDirective(io.leopard.web.freemarker.template.BodyTemplateDirective) ReplaceParamTemplateMethod(io.leopard.web.freemarker.template.ReplaceParamTemplateMethod)

Example 3 with TemplateVariable

use of io.leopard.web.freemarker.TemplateVariable in project leopard by tanhaichao.

the class TopnbView method getVariables.

@Override
public List<TemplateVariable> getVariables() {
    List<TemplateVariable> list = new ArrayList<TemplateVariable>();
    list.add(new TimeTemplateMethod());
    list.add(new AvgTimeTemplateMethod());
    list.add(new BodyTemplateDirective());
    list.add(new MenuTemplateDirective());
    list.add(new ServerInfoTemplateDirective());
    list.add(new ReplaceParamTemplateMethod());
    return list;
}
Also used : ArrayList(java.util.ArrayList) TemplateVariable(io.leopard.web.freemarker.TemplateVariable) TimeTemplateMethod(io.leopard.web.freemarker.template.TimeTemplateMethod) BodyTemplateDirective(io.leopard.web.freemarker.template.BodyTemplateDirective) ReplaceParamTemplateMethod(io.leopard.web.freemarker.template.ReplaceParamTemplateMethod)

Aggregations

TemplateVariable (io.leopard.web.freemarker.TemplateVariable)3 BodyTemplateDirective (io.leopard.web.freemarker.template.BodyTemplateDirective)2 ReplaceParamTemplateMethod (io.leopard.web.freemarker.template.ReplaceParamTemplateMethod)2 TimeTemplateMethod (io.leopard.web.freemarker.template.TimeTemplateMethod)2 ArrayList (java.util.ArrayList)2 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)1 Configuration (freemarker.template.Configuration)1 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)1 SimpleHash (freemarker.template.SimpleHash)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 ButtonAjaxTemplateDirective (io.leopard.web.freemarker.template.ajax.ButtonAjaxTemplateDirective)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Properties (java.util.Properties)1