model에 담긴 attribute를 출력하기 위해서는 {{attribute_name}}을 사용한다. 이 경우는 text 형태로 attribute의 내용을 단순 출력하는데 간혹 html 형태로 출력하려는 경우 {{{attribute_name}}} 형태나 {{&attribute_name}}을 사용하면 된다.
<ul><li>text로 출력: {{ org }}</li><li>html로 출력 1: {{{org}}}</li><li>html로 출력 2: {{&org}}</li></ul>
출력 결과는 아래와 같다.
한가지 아쉬운 점은 null이나 존재하지 않는 값을 사용하면 오류가 발생한다는 점이다. (공백이나 빈 문자열은 무관)
현재는 없는 값: {{ some }}
-- 오류
There was an unexpected error (type=Internal Server Error, status=500).
No method or field with name 'some' on line 15
com.samskivert.mustache.MustacheException$Context: No method or field with name 'some' on line 15
따라서 값을 확실히 채워서 보내던가 아래 나오는 section을 이용해야 한다.
section
section은 {{#attribute_name}}과 {{/attribute_name}} 형태를 사용하는데 attribute의 값에 따라서 동작이 달라진다.
true/false
model.getAttribute(attribute_name)값이 true/false인 경우는 조건문 처럼 동작한다. 따라서 true인 경우는 section이 동작하고 false인 경우는 동작하지 않는다. 이 동작은 값이 null인 경우와 null이 아닌 경우도 동일하다.
{{# notebook }}
<ul><li>{{ model }}</li><li>{{ price }}</li></ul>
{{/ notebook }}
{{# info }}
<ul><li>{{ name }}</li><li>{{ age }}</li></ul>
{{/ info }}
부정
section에서 ^를 사용하면 inverted section이 되어서 boolean 타입에 대해 부정을 선언할 수 있다.
{{^ false_value }}
아직 값이 없어요
{{/ false_value }}
inverted section은 boolean 뿐 아니라 collection에 대해서도 사용할 수 있다.
기타
include 처리
대부분 script 언어들은 header와 footer를 include 해서 처리한다. mustache역시 이런 기능을 제공하는데 {{> 삽입할페이지}} 형태를 사용한다.