BackEnd/Thymeleaf

타임리프 - 리터럴

연향동큰손 2024. 7. 16. 21:46

타임리프에서 문자 리터럴은 항상 ' '작은 따옴표로 감싸줘야 한다.

 

하지만 공백없이 이어지는 문자 리터럴은 작은 따옴표로 감싸지 않아도 된다.

ex) <span th : text = "hello">

 

아래 코드와 같이 공백이 있으면 오류가 생긴다.

<span th:text="hello world!">

 

작은 따옴표로 감싸면 다시 정상작동한다.

<span th:text="'hello' + ' world!'">

 

 

<literal.html>

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>리터럴</h1>
<ul>
    <!--주의! 다음 주석을 풀면 예외가 발생함-->
    <!--    <li>"hello world!" = <span th:text="hello world!"></span></li>-->
    <li>'hello' + ' world!' = <span th:text="'hello' + ' world!'"></span></li>
    <li>'hello world!' = <span th:text="'hello world!'"></span></li>
    <li>'hello ' + ${data} = <span th:text="'hello ' + ${data}"></span></li>
    <li>리터럴 대체 |hello ${data}| = <span th:text="|hello ${data}|"></span></li>
</ul>
</body>
</html>

 

리터럴 대체(Literal substitutions)

"|hello ${data}|"

 

리터럴 대체 문법을 사용하면 템플릿을 사용하는 것 처럼 편리하게 이용할 수 있다.

 

 

 

 

<실행결과>

'BackEnd > Thymeleaf' 카테고리의 다른 글

타임리프-반복문  (0) 2024.07.19
타임리프-속성값 설정  (0) 2024.07.19
타임리프-URL 링크  (0) 2024.07.16
타임리프-기본객체  (0) 2024.07.16
변수 - SpringEL  (1) 2024.07.15