Search in sources :

Example 71 with Template

use of freemarker.template.Template in project bamboobsc by billchen198318.

the class DynamicHqlUtils method process.

public static String process(String resource, String queryName, Map<String, Object> paramMap) throws Exception {
    DynamicHql dynamicHql = loadResource(resource);
    if (null == dynamicHql) {
        logger.error("no dynamic hql resource.");
        throw new Exception("no dynamic hql resource.");
    }
    String hql = "";
    for (int i = 0; i < dynamicHql.getQuery().size() && hql.length() < 1; i++) {
        Query queryObj = dynamicHql.getQuery().get(i);
        if (!queryObj.getName().equals(queryName)) {
            continue;
        }
        StringTemplateLoader templateLoader = new StringTemplateLoader();
        templateLoader.putTemplate(TEMPLATE_ID, queryObj.getContent());
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
        cfg.setTemplateLoader(templateLoader);
        Template template = cfg.getTemplate(TEMPLATE_ID, Constants.BASE_ENCODING);
        Writer out = new StringWriter();
        template.process(paramMap, out);
        hql = out.toString();
    }
    if (StringUtils.isBlank(hql)) {
        logger.warn("hql is blank.");
    }
    return hql;
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) Query(com.netsteadfast.greenstep.base.model.dynamichql.Query) Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) DynamicHql(com.netsteadfast.greenstep.base.model.dynamichql.DynamicHql) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Template(freemarker.template.Template)

Example 72 with Template

use of freemarker.template.Template in project engine by craftercms.

the class RenderComponentDirective method execute.

@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
    TemplateModel componentParam = (TemplateModel) params.get(COMPONENT_PARAM_NAME);
    TemplateModel componentPathParam = (TemplateModel) params.get(COMPONENT_PATH_PARAM_NAME);
    TemplateModel additionalModelParam = (TemplateModel) params.get(ADDITIONAL_MODEL_PARAM_NAME);
    Map<String, Object> additionalModel = null;
    SiteItem component;
    if (componentParam == null && componentPathParam == null) {
        throw new TemplateException("No '" + COMPONENT_PARAM_NAME + "' or '" + COMPONENT_PATH_PARAM_NAME + "' param specified", env);
    } else if (componentParam != null) {
        component = getComponentFromNode(componentParam, env);
    } else {
        component = getComponentFromPath(componentPathParam, env);
    }
    if (additionalModelParam != null) {
        additionalModel = unwrap(ADDITIONAL_MODEL_PARAM_NAME, additionalModelParam, Map.class, env);
    }
    Map<String, Object> templateModel = executeScripts(component, additionalModel, env);
    SimpleHash model = getFullModel(component, templateModel, additionalModel);
    Template template = getTemplate(component, env);
    Writer output = env.getOut();
    processComponentTemplate(template, model, output, env);
}
Also used : TemplateException(freemarker.template.TemplateException) SimpleHash(freemarker.template.SimpleHash) TemplateModel(freemarker.template.TemplateModel) HashMap(java.util.HashMap) Map(java.util.Map) SiteItem(org.craftercms.engine.model.SiteItem) Writer(java.io.Writer) Template(freemarker.template.Template)

Aggregations

Template (freemarker.template.Template)72 IOException (java.io.IOException)33 StringWriter (java.io.StringWriter)32 Configuration (freemarker.template.Configuration)30 Writer (java.io.Writer)26 HashMap (java.util.HashMap)26 TemplateException (freemarker.template.TemplateException)23 OutputStreamWriter (java.io.OutputStreamWriter)13 File (java.io.File)9 Map (java.util.Map)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 JSONObject (org.json.JSONObject)6 SimpleHash (freemarker.template.SimpleHash)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 StringTemplateLoader (freemarker.cache.StringTemplateLoader)4 Environment (freemarker.core.Environment)4 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)4 BufferedWriter (java.io.BufferedWriter)4 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4