freemarker 定义模板layout

定义下面类, 会在以后模板中使用到

package com.vogella.freemarker.first;

public class ValueExampleObject {

  private String name;
  private String developer;

  public ValueExampleObject(String name, String developer) {
    this.name = name;
    this.developer = developer;
  }

  public String getName() {
    return name;
  }

  public String getDeveloper() {
    return developer;
  }

}
package com.vogella.freemarker.first;

import java.io.File;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.Version;

public class MainTest {

  public static void main(String[] args) throws Exception {

    // 1. Configure FreeMarker
    //
    // You should do this ONLY ONCE, when your application starts,
    // then reuse the same Configuration object elsewhere.
    
    Configuration cfg = new Configuration();
    
    // Where do we load the templates from:
    cfg.setClassForTemplateLoading(MainTest.class, "templates");
    
    // Some other recommended settings:
    cfg.setIncompatibleImprovements(new Version(2, 3, 20));
    cfg.setDefaultEncoding("UTF-8");
    cfg.setLocale(Locale.US);
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

    // 2. Proccess template(s)
    //
    // You will do this for several times in typical applications.
    
    // 2.1. Prepare the template input:
    
    Map<String, Object> input = new HashMap<String, Object>();
    
    input.put("title", "Vogella example");

    input.put("exampleObject", new ValueExampleObject("Java object", "me"));
    
    List<ValueExampleObject> systems = new ArrayList<ValueExampleObject>();
    systems.add(new ValueExampleObject("Android", "Google"));
    systems.add(new ValueExampleObject("iOS States", "Apple"));
    systems.add(new ValueExampleObject("Ubuntu", "Canonical"));
    systems.add(new ValueExampleObject("Windows7", "Microsoft"));
    input.put("systems", systems);
    
    // 2.2. Get the template

    Template template = cfg.getTemplate("helloworld.ftl");
      
    // 2.3. Generate the output

    // Write output to the console
    Writer consoleWriter = new OutputStreamWriter(System.out);
    template.process(input, consoleWriter);

    // For the sake of example, also write output into a file:
    Writer fileWriter = new FileWriter(new File("output.html"));
    try {
      template.process(input, fileWriter);
    } finally {
      fileWriter.close();
    }

  }
}

定义layout--utils.ftl

<#macro page>
  <html>
  <head>
    <title>${title}</title>
  </head>
  <body>
    <h1>${title}</h1>
  
    <#-- This processes the enclosed content:  -->
    <#nested>
  </body>
  </html>
</#macro>

<#macro otherExample p1 p2>
  <p>The parameters were: ${p1}, ${p2}</p>
</#macro>

创建helloworld.ftl 调用模板 layout--utils.ftl

<#import "lib/utils.ftl" as u> 

<@u.page>
  <p>${exampleObject.name} by ${exampleObject.developer}</p>

  <ul>
    <#list systems as system>
      <li>${system_index + 1}. ${system.name} from ${system.developer}</li>
    </#list>
  </ul>
  
  <#-- Just another example of using a macro: -->
  <@u.otherExample p1=11 p2=22 />
</@u.page>

相关推荐