Package freemarker.template

Examples of freemarker.template.Template


*/
public class FreemarkerTemplateTests extends TestCase {

    public void testGenerate() throws Exception {
        Configuration conf = new Configuration();
        Template template = new Template("name", new StringReader("${name}"), conf);
        FreemarkerTemplate ft = new FreemarkerTemplate(template);

        Map model = new HashMap();
        model.put("name", "Daan");
        String name = ft.generate(model);
View Full Code Here


      root.put("modelMetadata", modelMetadata);
     
      /*
       *  使用Configuration实例来加载指定模板
       */
      Template template = CONFIG.getTemplate(templateFile);
      template.setEncoding(modelMetadata.getEncoding());
     
      /*
       * 代码目录与文件
       */
      File fileDir = new File(codeFilePath);
      if(!fileDir.exists()) {
        fileDir.mkdirs();
      }
      File codeFile = new File(codeFilePath + File.separator + codeFileName);
      System.out.println("generate file " + codeFile);
      FileOutputStream fos = new FileOutputStream(codeFile);
      OutputStreamWriter out = new OutputStreamWriter(fos, modelMetadata.getEncoding());
      BufferedWriter bw = new BufferedWriter(out);
     
      /*
       * 合并处理数据与模型
       */
      template.process(root, bw);
      bw.flush();
      bw.close();
     
      return codeFile.getAbsolutePath();
    } catch (Exception e) {
View Full Code Here

    private void executeMacro(Appendable writer, String macro) throws IOException {
        try {
            Environment environment = getEnvironment(writer);
            Reader templateReader = new StringReader(macro);
            // FIXME: I am using a Date as an hack to provide a unique name for the template...
            Template template = new Template((new java.util.Date()).toString(), templateReader, FreeMarkerWorker.getDefaultOfbizConfig());
            templateReader.close();
            environment.include(template);
        } catch (TemplateException e) {
            Debug.logError(e, "Error rendering screen macro [" + macro + "] thru ftl", module);
        } catch (IOException e) {
View Full Code Here

        // check for a cached template
        if (cache) {
            String disableCache = UtilProperties.getPropertyValue("content", "disable.ftl.template.cache");
            if (disableCache == null || !disableCache.equalsIgnoreCase("true")) {
                try {
                    Template cachedTemplate = FreeMarkerWorker.getTemplate("DataResource:" + dataResourceId);
                    if (cachedTemplate != null) {
                        String subContentId = (String) templateContext.get("subContentId");
                        if (UtilValidate.isNotEmpty(subContentId)) {
                            templateContext.put("contentId", subContentId);
                            templateContext.put("subContentId", null);
View Full Code Here

    private void executeMacro(Appendable writer, String macro) throws IOException {
        try {
            Reader templateReader = new StringReader(macro);
            // FIXME: I am using a Date as an hack to provide a unique name for the template...
            Template template = new Template((new java.util.Date()).toString(), templateReader, FreeMarkerWorker.getDefaultOfbizConfig());
            templateReader.close();
            if (writer != null) {
                Map<String, Object> input = UtilMisc.toMap("key", null);
                Environment tmpEnvironment = FreeMarkerWorker.renderTemplate(this.macroLibrary, input, writer);
                tmpEnvironment.include(template);
View Full Code Here

                if (insertWidgetBoundaryComments) {
                    writer.append(HtmlWidgetRenderer.formatBoundaryComment("Begin", "Template", location));
                }

                //FreeMarkerWorker.renderTemplateAtLocation(location, context, writer);
                Template template = null;
                if (location.endsWith(".fo.ftl")) { // FOP can't render correctly escaped characters
                    template = FreeMarkerWorker.getTemplate(location);
                } else {
                    template = FreeMarkerWorker.getTemplate(location, specialTemplateCache, specialConfig);
                }
View Full Code Here

        templateContext.put("additionalFields", passThru);
        templateContext.put("defaultValues", defaultValues);
        templateContext.put("delegator", this.delegator);
        templateContext.put("locale", Locale.getDefault());

        Template template = this.getTemplate(templateUrl);
        try {
            FreeMarkerWorker.renderTemplate(template, templateContext, writer);
        } catch (TemplateException e) {
            Debug.logError(e, "Error rendering Survey with template at [" + templateUrl.toExternalForm() + "]", module);
        } catch (IOException e) {
View Full Code Here

    // returns the FTL Template object
    // Note: the template will not be cached
    protected Template getTemplate(URL templateUrl) {
        Configuration config = FreeMarkerWorker.getDefaultOfbizConfig();

        Template template = null;
        try {
            InputStream templateStream = templateUrl.openStream();
            InputStreamReader templateReader = new InputStreamReader(templateStream);
            template = new Template(templateUrl.toExternalForm(), templateReader, config);
        } catch (IOException e) {
            Debug.logError(e, "Unable to get template from URL :" + templateUrl.toExternalForm(), module);
        }
        return template;
    }
View Full Code Here

        NodeModel nodeModel = NodeModel.wrap(domNode);
        Map<String, Object> ctx = FastMap.newInstance();
        ctx.put("doc", nodeModel);
        ctx.put("entityName", entityName);
        StringWriter outWriter = new StringWriter();
        Template template = getDocTemplate(templatePath);
        template.process(ctx, outWriter);
        outWriter.close();
        result = outWriter.toString();
        return result;
    }
View Full Code Here

        result = outWriter.toString();
        return result;
    }

    public static Template getDocTemplate(String fileUrlthrows FileNotFoundException, IOException, TemplateException, URISyntaxException {
        Template template = null;
        URL screenFileUrl = FlexibleLocation.resolveLocation(fileUrl, null);
        String urlStr = screenFileUrl.toString();
        URI uri = new URI(urlStr);
        File f = new File(uri);
        FileReader templateReader = new FileReader(f);
        Configuration conf = makeDefaultOfbizConfig();
        template = new Template("FMImportFilter", templateReader, conf);
        return template;
    }
View Full Code Here

TOP

Related Classes of freemarker.template.Template

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.