๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/NodeJS

[NodeJS+Express] 8. EJS ๋ฌธ๋ฒ• ์ •๋ฆฌ

by KASSID 2023. 10. 9.

๋ชฉ์ฐจ

    728x90

    ์ด์ „ 2๊ฐœ์˜ ํฌ์ŠคํŠธ์—์„œ ๋‹ค๋ฃจ์—ˆ๋˜ EJS ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด๋ณด์ž!

     

    1. <% %>

    - JS ๋ฌธ๋ฒ•์„ EJS ํŒŒ์ผ ๋‚ด์—์„œ ์‚ฌ์šฉํ•  ๋•Œ

     

    ์˜ˆ์‹œ

    <% for (let i = 0; i < 5; i++){ %>
      <div>HELLO!</div>
    <% } %>

     

    ๊ฒฐ๊ณผ

     

    2. <%= %>

    - <%= %> ์‚ฌ์ด์— ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ html์ด์–ด๋„ escapeํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅ

     

    ์˜ˆ์‹œ

    <div class="post-title-container">
      <h4 class="post-title">
        <%= list[i].title%>
      </h4>
    </div>
    <div class="post-content-container">
      <p class="post-content">
        <%= list[i].content%>
      </p>
    </div>

     

    ๊ฒฐ๊ณผ

     

    3. <%- %>

    - <%- %> ์‚ฌ์ด์— ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ html์ด๋ผ๋ฉด ์‹ค์ œ html ๋ธ”๋ก ์ƒ์„ฑ

       (์ฆ‰, include() ๋“ฑ ํŠน์ˆ˜ํ•œ ๋ฌธ๋ฒ• ์‚ฌ์šฉ ํ˜น์€ DB์—์„œ html ๋ฐ›์•„์˜ฌ ๋•Œ ํ™œ์šฉ)

     

    ์˜ˆ์‹œ

    <%- include('footer.ejs')%>

     

    //footer.ejs
    <footer>
      <div>@KassidBoard</div>
    </footer>

     

    ๊ฒฐ๊ณผ

     

     

    ๋Œ“๊ธ€