본문 바로가기
기술 부채 상환 중....

벨로시티에서 프리마커로 갈아타기

by 닮은 2018. 4. 22.

자세한 것은 여기서 보시면 됩니다.

여기

설정

대충 보기

변수

    ${value}
  • $value -> ${value} 반드시 중괄호로 감싸야 한다.
  • ${StringUtil.isEmpty($value)} -> ${StringUtil.isEmpty(value)}
  • #set($value = "velocity") -> <#assign value = "freemarker">

리스트

    <#list list as item>
        ${item.value}
    </#list>

조건문

    <#if(value == true)>
        ${value.string('true','')}
    </#if>
  • 변수가 있는지는 확인하려면 ??를 쓰면 된다. <#if value??>this value is not null</#if>

매크로

    <#macro layout title>
      <html>
          <head>
            <title>${title}</title>
        </head>
        <body>
            <#nested>
        </body>
      </html>
    </#macro>

    <@layout>
        content
    </@layout>

include

  • 벨로시티의 #parse
  • #parse("header.vm") -> #include("header.ftl")

BeansWrapper

  • toolbox 대용(?)으로 이렇게 설정하면 ${statics["java.lang.System"].currentTimeMillis()} 이런식으로 쓸 수 있다.
    BeansWrapper w = new BeansWrapper(); 
    TemplateHashModel statics = w.getStaticModels();
    model.addAttribute("statics", statics);

삽질 방지

변수 비교

  • <#if value > 0> 라고 쓴다면 크다의 > 를 프리마커 닫는 태그로 인식하기 때문에 gt를 쓰거나 <#if value gt 0> 비교 하는 부분을 괄호 <#if (value > 0)>로 감싸주어야 한다.

숫자

  • 별도로 설정을 바꾸지 않은 경우에 숫자에 comma가 찍혀서 나온다;
  • number?c c는 computer라는 뜻인데 이렇게 해야 comma가 제거되어서 나온다.
  • 모든 숫자값에 comma가 안 찍히게 보여주고 싶으면 파일 설정을 바꾸면 된다. <#setting number_format="computer">

인코딩

  • 자동 인코딩 <#ftl output_format="HTML" auto_esc=true>
  • 파일 자체에 auto_esc 설정을 했으나, escape 원하지 않는 경우에는 ${value?no_esc}

꽤 괜찮았던 벨로시티

벨로시티 레이아웃 설정 있어요...

  • 이렇게 기본 레이아웃을 설정해주면 $screen_content 여기에 본문이 들어가게 된다.
    <html>
        <head>
            <title>Velocity</title>
        </head>
        <body>
            #parse("/fragments/header.vm")
                $screen_content
            #parse("/fragments/footer.vm")
        </body>
    </html>
  • 다른 레이아웃을 쓰고 싶으면
    #set($layout = "myOtherLayout.vm")
  • 안 쓰고 싶으면 noLayout.vm을 아래와 같이 만들어면; 된다.
    $screen_content

일단은 뭐라도 찍어줍니다...

  • 벨로시티는 변수가 없더라도 ${value} 그대로 찍어주긴 하는데 프리마커에서를 렌더링 에러가 난다. <#if value??>${value}</#if> 이렇게 한땀 한땀 해야한다.

타입

  • 벨로시티에서는 타입이 헷갈리면 #if ('${number}' == '${str}' 라고 해버리면 되는데 프리마커에서는 타입이 다른 변수끼리 비교하려고 한다는 렌더링 에러가 난다. number가 number 구나.. 를 확인하고 와서 <#if number1?string == str> 이렇게 해야하는 번거로움이 있다ㅠ

참고