Package freemarker.template

Examples of freemarker.template.Template


   * @return
   */
  public static String sql(String groupNameAndsqlId,Map<String,Object>paras){
    try{
      StringReader reader=new StringReader(sqlMap.get(groupNameAndsqlId));
      Template temp=new Template("", reader);
      StringWriter out=new StringWriter();
      temp.process(paras, out);
      return out.toString();
    }catch(Exception e){}
    return "";
  }
View Full Code Here


                //
                // Get the subject
                //
                //BodyPart subjectPart = new MimeBodyPart();
                Template subjectTextTemplate = configuration.getTemplate(subjectTemplatePrefix);
                final StringWriter subjectTextWriter = new StringWriter();
               
                try {
                  subjectTextTemplate.process(map, subjectTextWriter);
                } catch (TemplateException e) {
                    throw new MailPreparationException("Can't generate Subject Text", e);
                }        
                mimeMessage.setSubject(subjectTextWriter.toString());     
               
                //
                // Create a "text" Multipart message
                //
               
                Template bodyTextTemplate = configuration.getTemplate(bodyTemplatePrefix);
                final StringWriter bodyTextWriter = new StringWriter();
               
               
                try {
                  bodyTextTemplate.process(map, bodyTextWriter);
                } catch (TemplateException e) {
                    throw new MailPreparationException("Can't generate Body Text", e);
                }
                mimeMessage.setText(bodyTextWriter.toString());
               
View Full Code Here

       
     // freemark
        StringWriter writer = new StringWriter();
        Configuration configuration = new Configuration();
        configuration.setTemplateLoader(new ClassTemplateLoader(PerformanceTest.class, "/"));
        Template template = configuration.getTemplate("books.ftl");
        template.process(context, writer);
//        byte[] ret = null;
        long start = System.currentTimeMillis();
        for (int i = 0; i < times; i++) {
          writer = new StringWriter();
          template.process(context, writer);
          writer.toString().getBytes("UTF-8");
    }
        long end = System.currentTimeMillis();
        System.out.println("freemark: " + (end - start) + "ms\t" + (int)(times / (double)(end - start) * 1000) + "tps");
//        System.out.println(new String(ret, "UTF-8"));
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

        if (!deployedErrorTemplate && templatePath.equals(NOT_FOUND_PAGE_PATH)) {
            templatePath = "META-INF/resources" + NOT_FOUND_PAGE_PATH;
        }

        // Get the template object
        Template template = configuration.getTemplate(templatePath);

        // Merge the data-model and the template
        template.process(model, writer);
    }
View Full Code Here

     */
    public void renderTemplate(String templatePath, Map model, Writer writer)
            throws Exception {

        // Get the template object
        Template template = configuration.getTemplate(templatePath);

        // Merge the data-model and the template
        template.process(model, writer);
    }
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 thru ftl", module);
        } catch (IOException e) {
View Full Code Here

        if (log.isDebugEnabled()) {
            log.debug("Freemarker is evaluating " + path + " using context: " + variableMap);
        }

        // let freemarker parse and generate the result in buffer
        Template template;
        if (encoding != null) {
            template = configuration.getTemplate(path, encoding);
        } else {
            template = configuration.getTemplate(path);
        }
        StringWriter buffer = new StringWriter();
        template.process(variableMap, buffer);
        buffer.flush();

        // now lets output the results to the exchange
        Message out = exchange.getOut(true);
        out.setBody(buffer.toString());
View Full Code Here

        if (!locationArg.startsWith("/")) {
            String base = ResourceUtil.getResourceBase(req);
            locationArg = base + "/" + locationArg;
        }

        Template template = configuration.getTemplate(locationArg, deduceLocale());
        TemplateModel model = createModel();

        // Give subclasses a chance to hook into preprocessing
        if (preTemplateProcess(template, model)) {
            try {
                // Process the template
                Writer writer = getWriter();
                if (isWriteIfCompleted() || configuration.getTemplateExceptionHandler() == TemplateExceptionHandler.RETHROW_HANDLER) {
                    CharArrayWriter parentCharArrayWriter = (CharArrayWriter) req.getAttribute(PARENT_TEMPLATE_WRITER);
                    boolean isTopTemplate = false;
                    if (isTopTemplate = (parentCharArrayWriter == null)) {
                        //this is the top template
                        parentCharArrayWriter = new CharArrayWriter();
                        //set it in the request because when the "action" tag is used a new VS and ActionContext is created
                        req.setAttribute(PARENT_TEMPLATE_WRITER, parentCharArrayWriter);
                    }

                    try {
                        template.process(model, parentCharArrayWriter);

                        if (isTopTemplate) {
                            parentCharArrayWriter.flush();
                            parentCharArrayWriter.writeTo(writer);
                        }
                    } catch (TemplateException e) {
                        if (LOG.isErrorEnabled()) {
                            LOG.error("Error processing Freemarker result!", e);
                        }
                        throw e;
                    } catch (IOException e) {
                        if (LOG.isErrorEnabled()){
                            LOG.error("Error processing Freemarker result!", e);
                        }
                        throw e;
                    } finally {
                        if (isTopTemplate && parentCharArrayWriter != null) {
                            req.removeAttribute(PARENT_TEMPLATE_WRITER);
                            parentCharArrayWriter.close();
                        }
                    }
                } else {
                    template.process(model, writer);
                }
            } finally {
                // Give subclasses a chance to hook into postprocessing
                postTemplateProcess(template, model);
            }
View Full Code Here

            }
            try {
                FreemarkerManager mgr = getContainer().getInstance(FreemarkerManager.class);

                freemarker.template.Configuration config = mgr.getConfiguration(ctx);
                Template template = config.getTemplate("/org/apache/struts2/dispatcher/error.ftl");

                List<Throwable> chain = new ArrayList<Throwable>();
                Throwable cur = e;
                chain.add(cur);
                while ((cur = cur.getCause()) != null) {
                    chain.add(cur);
                }

                HashMap<String,Object> data = new HashMap<String,Object>();
                data.put("exception", e);
                data.put("unknown", Location.UNKNOWN);
                data.put("chain", chain);
                data.put("locator", new Locator());

                Writer writer = new StringWriter();
                template.process(data, writer);

                response.setContentType("text/html");
                response.getWriter().write(writer.toString());
                response.getWriter().close();
            } catch (Exception exp) {
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.