u16suzuの blog

日々学んだことのメモブログです。

ftl FreeMarker

変数定義

<#assign hoge=33>

変数表示(↑で定義した場合)

${hoge}

マクロ定義

<#macro name arg arg2="">

マクロ呼び出し

(外部ファイルでマクロを定義した時はincludeが必要なことに注意)
<#include 'macros.ftl'/>
<@macro_name arg="hoge" arg2="fuga">

制御文

<#if a==33>
<#else>

<#if a==b>
<#elseif b == 33>

switch-case文

<#switch being.size>
  <#case "small">
     This will be processed if it is small
     <#break>
  <#case "medium">
     This will be processed if it is medium
     <#break>
  <#case "large">
     This will be processed if it is large
     <#break>
  <#default>
     This will be processed if it is neither
 

for each

<#list dog_list as dog>
${dog}

コメント
<#--- comment -->
存在するか

<#if a??>

存在しないか

<#if !a??>

has_content

「??」は変数が存在すればtrueを返す
「?has_content」は変数が存在してかつ中身が空でないときにtrueを返す

エラーコレクション

freemarker.core.ParseException: Unexpected end of file reached.