What is FreeMarker?
FreeMarker is a "template engine"; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It's a Java package, a class library for Java programmers. It's not an application for end-users in itself, but something that programmers can embed into their products.

Template + data model = output
Ubiquitous Language : 용어정리를 해보자
- We say that the output is created by merging a template and a data model
- Files like this are called templates.
Templates
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>
Welcome ${user}<#if user = "Big Joe">, our beloved leader</#if>!
</h1>
<p>Our latest product:
<a href="${latestProduct.url}">${latestProduct.name}</a>!
</body>
</html> Data Model : the data model is basically a tree

|
Directive
* 조건문만 알면 어떤 랭귀지라도 대강 코딩할 수 있죠? 다음과 같은 syntax입니다.
Synopsis
<#if condition>
...
<#elseif condition2>
...
<#elseif condition3>
...
...
<#else>
...
</#if>
...
<#elseif condition2>
...
<#elseif condition3>
...
...
<#else>
...
</#if>
* 기대하고 기대하는 loop사용하는 방법
<#list SequenceVar as variable>repeatThis</#list>
|
* include
|
* Using directives together : 함께 사용할 수도 있슴
|
ouput을 만들기 위한 샘플코드
public String createTemplateContents(String fileName, Object root) throws Exception {
final Writer out = new StringWriter();
Configuration cfg = new Configuration();
cfg.setClassicCompatible(false);
cfg.setClassForTemplateLoading(getClass(), THEME_TEMPLATES_CLASSPATH);
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template temp = cfg.getTemplate(fileName);
temp.process(root, out);
out.flush();
return out.toString();
}
final Writer out = new StringWriter();
Configuration cfg = new Configuration();
cfg.setClassicCompatible(false);
cfg.setClassForTemplateLoading(getClass(), THEME_TEMPLATES_CLASSPATH);
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template temp = cfg.getTemplate(fileName);
temp.process(root, out);
out.flush();
return out.toString();
}
기타
(열심히 API학습중!)
오~ freemarker쓸만한데, TemplateModel 인터페이스는 감동이었다.
이거 잘 이용하면 DTO형태의 Proxy클래스를 만들지 않아도 되겠다.
Velocity에도 이런 기능이 있었을 텐데, 그땐 웹UI로만 사용을 해서 깊이 있게 못 봤는데, 앞으로 template engine은 freemarker를 사용하면서 좀더 깊이 있게 들여다 봐야 겠군.
일단 소기의 목적을 달성을 위한 만큼의 API공부는 달성!!
잘 만든 Domain Model클래스를 이용하여, 타입이 확실한 형태의 도메인 모델을 이용한 코드를 만들수도 있다. 그건 차후에 샘플을 올리도록 하겠다. 매번 property의 이름을 찾아서 Map에 담는 형태보다는 사전에 정의된 Bean을 이용하면 intuitable populate가 가능하다.
http://freemarker.sourceforge.net/
http://freemarker.sourceforge.net/docs/api/index.html
http://freemarker.sourceforge.net/docs/dgui_quickstart_datamodel.html
