Programming/Node.js

EJS (Embedded JavaScript templating) - Template Engine

junnnhhh 2024. 1. 30. 22:14
728x90

EJS란 Node.js에서 사용하는 템플릿 엔진이다.

 

정적인 HTML만 사용하는 경우에, 뷰를 호출하면서 동적 처리가 불가하므로 수고로운 작업을

거쳐야 한다.

 

그러나, EJS를 쓰게 되면, 뷰를 호출하는 동시에 변수를 넘겨주어 Node에서 HTML로 넘긴 변수를
할 수 있게 된다.

 

ejs 기본 문법

<% if (user) { %>
  <h2><%= user.name %></h2>
<% } %>

// <% ... %> 자바스크립트를 작성할 수 있다.
// <%= ... %> 전달받은 변수를 사용할 수 있다.

 

Ejs Tags

  • <% 'Scriptlet' tag, for control-flow, no output
  • <%_ ‘Whitespace Slurping’ Scriptlet tag, strips all whitespace before it
  • <%= Outputs the value into the template (HTML escaped)
  • <%- Outputs the unescaped value into the template
  • <%# Comment tag, no execution, no output
  • <%% Outputs a literal '<%'
  • %> Plain ending tag
  • -%> Trim-mode ('newline slurp') tag, trims following newline
  • _%> ‘Whitespace Slurping’ ending tag, removes all whitespace after it

https://ejs.co/#install

 

EJS -- Embedded JavaScript templates

Simple syntax JavaScript code in simple, straightforward scriptlet tags. Just write JavaScript that emits the HTML you want, and get the job done!

ejs.co

더 자세한 내용은 공식 사이트를 통해 확인해보자.

728x90