Search in sources :

Example 31 with Template

use of freemarker.template.Template in project SpringStepByStep by JavaProgrammerLB.

the class FreemarkerTest method main.

public static void main(String[] args) {
    Configuration config = new Configuration();
    try {
        String path = new File("").getAbsolutePath();
        config.setDirectoryForTemplateLoading(new File(path));
        Template template = config.getTemplate("src/test.ftl", "UTF-8");
        // 创建数据模型
        Map root = new HashMap();
        List<User> users = new ArrayList<User>();
        User u1 = new User();
        u1.setId("123");
        u1.setName("王五");
        users.add(u1);
        User u2 = new User();
        u2.setId("423");
        u2.setName("李四");
        users.add(u2);
        User u3 = new User();
        u3.setId("333");
        u3.setName("张三");
        users.add(u3);
        root.put("userList", users);
        Map product = new HashMap();
        root.put("lastProduct", product);
        product.put("url", "www.baidu.com");
        product.put("name", "green hose");
        File file = new File(path + "\\src\\test.html");
        if (!file.exists()) {
            //System.out.println("file exist");  
            file.createNewFile();
        }
        Writer out = new BufferedWriter(new FileWriter(file));
        template.process(root, out);
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TemplateException e) {
        e.printStackTrace();
    }
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Template(freemarker.template.Template) BufferedWriter(java.io.BufferedWriter) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 32 with Template

use of freemarker.template.Template in project SpringStepByStep by JavaProgrammerLB.

the class FreeMarkerTemplateEngine method render.

@Override
public String render(ModelAndView modelAndView) {
    try {
        StringWriter stringWriter = new StringWriter();
        Template template = configuration.getTemplate(modelAndView.getViewName());
        template.process(modelAndView.getModel(), stringWriter);
        return stringWriter.toString();
    } catch (IOException e) {
        throw new IllegalArgumentException();
    } catch (TemplateException e) {
        throw new IllegalArgumentException();
    }
}
Also used : StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 33 with Template

use of freemarker.template.Template in project android by JetBrains.

the class StringEvaluator method evaluate.

/** Evaluates the given expression, with the given set of arguments */
@Nullable
public String evaluate(@NonNull String expression, @NonNull Map<String, Object> inputs) {
    try {
        myCurrentExpression = expression;
        Template inputsTemplate = myFreemarker.getTemplate(expression);
        StringWriter out = new StringWriter();
        Map<String, Object> args = FreemarkerUtils.createParameterMap(inputs);
        inputsTemplate.process(args, out);
        out.flush();
        return out.toString();
    } catch (Exception e) {
        return null;
    }
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) Template(freemarker.template.Template) Nullable(com.android.annotations.Nullable)

Example 34 with Template

use of freemarker.template.Template in project jmeter by apache.

the class TemplateVisitor method visitFile.

/*
     * (non-Javadoc)
     * 
     * @see java.nio.file.SimpleFileVisitor#visitFile(java.lang.Object,
     * java.nio.file.attribute.BasicFileAttributes)
     */
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    // Depending on file extension, copy or process file
    String extension = FilenameUtils.getExtension(file.toString());
    if (TEMPLATED_FILE_EXT.equalsIgnoreCase(extension)) {
        // Process template file
        String templatePath = source.relativize(file).toString();
        Template template = configuration.getTemplate(templatePath);
        Path newPath = target.resolve(FilenameUtils.removeExtension(templatePath));
        try (FileOutputStream stream = new FileOutputStream(newPath.toString());
            Writer writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
            BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
            template.process(data, bufferedWriter);
        } catch (TemplateException ex) {
            throw new IOException(ex);
        }
    } else {
        // Copy regular file
        Path newFile = target.resolve(source.relativize(file));
        Files.copy(file, newFile, StandardCopyOption.REPLACE_EXISTING);
    }
    return FileVisitResult.CONTINUE;
}
Also used : Path(java.nio.file.Path) TemplateException(freemarker.template.TemplateException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Template(freemarker.template.Template) BufferedWriter(java.io.BufferedWriter)

Example 35 with Template

use of freemarker.template.Template in project sling by apache.

the class FreemarkerScriptEngine method eval.

public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
    final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
    if (helper == null) {
        throw new ScriptException("SlingScriptHelper missing from bindings");
    }
    // ensure GET request
    if (!"GET".equals(helper.getRequest().getMethod())) {
        throw new ScriptException("FreeMarker templates only support GET requests");
    }
    freemarkerScriptEngineFactory.getTemplateModels().forEach(bindings::put);
    final String scriptName = helper.getScript().getScriptResource().getPath();
    try {
        final Template template = new Template(scriptName, reader, configuration);
        template.process(bindings, scriptContext.getWriter());
    } catch (Throwable t) {
        final String message = String.format("Failure processing FreeMarker template %s.", scriptName);
        logger.error(message, t);
        throw new ScriptException(message);
    }
    return null;
}
Also used : ScriptException(javax.script.ScriptException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) Template(freemarker.template.Template)

Aggregations

Template (freemarker.template.Template)81 StringWriter (java.io.StringWriter)35 IOException (java.io.IOException)34 Configuration (freemarker.template.Configuration)33 HashMap (java.util.HashMap)28 Writer (java.io.Writer)27 TemplateException (freemarker.template.TemplateException)24 OutputStreamWriter (java.io.OutputStreamWriter)13 File (java.io.File)9 Map (java.util.Map)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 SimpleHash (freemarker.template.SimpleHash)6 JSONObject (org.json.JSONObject)6 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 StringTemplateLoader (freemarker.cache.StringTemplateLoader)4 Environment (freemarker.core.Environment)4 BufferedWriter (java.io.BufferedWriter)4 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4