BLOG main image
정민군's Blog
miniya devlog

What is FreeMarker?

Language/Java 2007. 7. 27. 18:19 by 정민군

출처 : http://www.ologist.co.kr/414


과거에는 velocity가 주로 템플릿 엔진으로 사용이 됐는데, 최근에는 webwork와 함께 FreeMarker가 주목을 많이 받고 있는듯 하다.

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

User inserted image



(root)
  |
  +- user = "Big Joe"
  |
  +- latestProduct
      |
      +- url = "products/greenmouse.html"
      |
      +- name = "green mouse"  



Directive

* 조건문만 알면 어떤 랭귀지라도 대강 코딩할 수 있죠? 다음과 같은 syntax입니다.
Synopsis

<#if condition>
  ...
<#elseif condition2>
  ...
<#elseif condition3>
  ...
...
<#else>
  ...
</#if>



* 기대하고 기대하는 loop사용하는 방법
<#list SequenceVar as variable>repeatThis</#list>

<p>And BTW we have these fruits:
<ul>
<#list whatnot.fruits as fruit> <li>${fruit} </#list> <ul>


* include
<html>
<head>
  <title>Test page</title>
</head>
<body>
  <h1>Test page</h1>
  <p>Blah blah...
<#include "/copyright_footer.html">
</body>
</html>  


* Using directives together : 함께 사용할 수도 있슴

<p>We have these animals:
<table border=1>
  <tr><th>Name<th>Price
  <#list animals as being>
  <tr>
    <td>
      <#if being.size = "large"><b></#if>
      ${being.name}
      <#if being.size = "large"></b></#if>
    <td>${being.price} Euros
  </#list>
</table>  


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();
}


카테고리

분류 전체보기 (46)
Technic (5)
Language (16)
Database (8)
System (5)
Algorithm (1)
Design (1)
Tool (3)
Framework (2)
Network (1)
Utility (1)
SmartPhone (2)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

달력

«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

글 보관함