How to Create a Template Method Model for FreeMarker

SampleTemplateMethodModel.java

import java.util.List;

import freemarker.template.TemplateMethodModel;

public final class SampleTemplateMethodModel implements TemplateMethodModel { // or TemplateMethodModelEx, if you want to receive template models instead of strings

  public Object exec(final List arguments) {
    return null;
  }

}

simple.ftl

<#assign sample="SampleTemplateMethodModel"?new()>
<#assign var=sample()>
${var}

http://freemarker.sourceforge.net/docs/api/freemarker/template/TemplateMethodModel.html

http://freemarker.sourceforge.net/docs/api/freemarker/template/TemplateMethodModelEx.html

http://freemarker.sourceforge.net/docs/ref_directive_assign.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s