프리마커 if else - peulimakeo if else

Synopsis

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

Where:

  • condition, condition2, ...etc.: Expression evaluates to a boolean value.

The elseif-s and the else are optional.

Camel case name variant: elseIf

Description

You can use if, elseif and else directives to conditionally skip a section of the template. The condition-s must evaluate to a boolean value, or else an error will abort template processing. The elseif-s and else-s must occur inside if (that is, between the if start-tag and end-tag). The if can contain any number of elseif-s (including 0) and at the end optionally one else. Examples:

if with 0 elseif and no else:

Template

<#if x == 1> x is 1 </#if>

if with 0 elseif and else:

Template

<#if x == 1> x is 1 <#else> x is not 1 </#if>

if with 2 elseif and no else:

Template

<#if x == 1> x is 1 <#elseif x == 2> x is 2 <#elseif x == 3> x is 3 </#if>

if with 3 elseif and else:

Template

<#if x == 1> x is 1 <#elseif x == 2> x is 2 <#elseif x == 3> x is 3 <#elseif x == 4> x is 4 <#else> x is not 1 nor 2 nor 3 nor 4 </#if>

To see more about boolean expressions, see: Template Author's Guide/The Template/Expressions.

You can nest if directives (of course):

Template

<#if x == 1> x is 1 <#if y == 1> and y is 1 too <#else> but y is not </#if> <#else> x is not 1 <#if y < 0> and y is less than 0 </#if> </#if>

Note:

When you want to test if x > 0 or x >= 0, writing <#if x > 0> and <#if x >= 0> is WRONG, as the first > will close the #if tag. To work that around, write <#if x gt 0> or <#if gte 0>. Also note that if the comparison occurs inside parentheses, you will have no such problem, like <#if foo.bar(x > 0)> works as expected.

프리마커란?

FreeMarker는 독립 실행형 또는 서블릿 기반 Java 프로그램에서 사용할 수 있는 Java 기반 템플릿 엔진입니다. 

FreeMarker에서 템플릿을 정의합니다. 템플릿은 ${name}과 같은 자리 표시자와 조건, 루프 등과 같은 일부 논리도 포함되어 있다는 점을 제외하고는 원하는 출력이 포함된 텍스트 파일입니다. Java 프로그램에서 이러한 자리 표시자에 대한 실제 값을 제공하고 이 입력을 기반으로 최종 출력이 생성됩니다.

프리마커 if, else, elseif

템플릿의 섹션을 조건부로 건너뛰도록 지시하고 싶은 경우 if, else, elseif 구문을 사용합니다. 조건문에는 반드시 참 또는 거짓의 값이 테스트되어야합니다. 그렇지 않으면 오류가 발생하여 템플릿 처리가 중단됩니다. elseif 또는 else절은 반드시 if절 내부에 위치해야합니다. elseif 절의 갯수는 제한이 없습니다. 하지만 else절은 반드시 가장 마지막 elseif절 뒤에 위치해야합니다.

if with 0 elseif and no else:

<#if x == 1> x is 1 </#if>

if with 0 elseif and else:

<#if x == 1> x is 1 <#else> x is not 1 </#if>

if with 2 elseif and no else:

<#if x == 1> x is 1 <#elseif x == 2> x is 2 <#elseif x == 3> x is 3 </#if>

if with 3 elseif and else:

<#if x == 1> x is 1 <#elseif x == 2> x is 2 <#elseif x == 3> x is 3 <#elseif x == 4> x is 4 <#else> x is not 1 nor 2 nor 3 nor 4 </#if>

프리마커 null/undefined

Missing value test 연산자

구문: unsafe_expr?? 또는 (unsafe_expr)??

이 연산자는 값이 누락되었는지 여부를 알려줍니다. 그에 따라 결과는 참이거나 거짓입니다.

예시: mouse라는 변수가 존재하지 않는다고 가정합시다.

<#if mouse??> Mouse found <#else> No mouse found </#if> Creating mouse... <#assign mouse = "Jerry"> <#if mouse??> Mouse found <#else> No mouse found </#if>

실행결과

No mouse found Creating mouse... Mouse found

Default value 연산자

구문: unsafe_expr!default_expr 또는 unsafe_expr! 또는 (unsafe_expr)!default_expr 또는 (unsafe_expr)!

이 연산자를 사용하면 값이 누락된 경우의 기본값을 지정할 수 있습니다.

예시: mouse라는 변수가 존재하지 않는다고 가정합시다.

${mouse!"No mouse."} <#assign mouse="Jerry"> ${mouse!"No mouse."}

실행결과

No mouse. Jerry

만약 기본값을 생략한다면 출력 결과는 빈 문자열로 출력됩니다.

Toplist

최신 우편물

태그