EJS

해피데빙·2022년 9월 30일
0

render

let ejs = require('ejs');
let people = ['geddy', 'neil', 'alex'];
let html = ejs.render('<%= people.join(", "); %>', {people: people});
//template string, data를 넘기면 HTML이 생긴다

template file, data file을 주고 output file을 정의한다

ejs ./template_file.ejs -f data_file.json -o ./output.html

Docs

let template = ejs.compile(str, options); /
template(data);
// => Rendered HTML string

data와 option을 함께
ejs.render(str, data, options);
// => Rendered HTML string

파일 렌더링하는 방법
ejs.renderFile(filename, data, options, function(err, str){
    // str => Rendered HTML string
});

Options

  • cache : Compiled functions are cached, requires filename
  • filename : Used by cache to key caches, and for includes
  • root : Set project root for includes with an absolute path (e.g, /file.ejs). Can be array to try to resolve include from multiple directories.

Includes

  • Includes are relative to the template with the include call.
  • This requires the 'filename' option.

ex. "./views/users.ejs" , "./views/user/show.ejs"가 있으면
you would use <%- include('user/show'); %>.

You'll likely want to use the raw output tag (<%-) with your include to avoid double-escaping the HTML output.

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) : html에 변수를 넣는다
  • <%- : 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
profile
노션 : https://garrulous-gander-3f2.notion.site/c488d337791c4c4cb6d93cb9fcc26f17

0개의 댓글