컴퓨터/NodeJS

NodeJS ejs-locals

sayyesdoit 2023. 5. 24. 14:47
설치
npm install ejs-locals
설정
const engine = require('ejs-locals');

app.engine('ejs', engine);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
사용

layout.ejs

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="/public/styles/index.css">
    <%-stylesheets%>
    <title>타이틀</title>
  </head>
  <body>
    <nav>
      <a href="/"><button>홈</button></a>
<% menus.forEach(function(menu) { %>
      <a href="<%= menu.url %>"><button><%= menu.name %></button></a>
<% }) %>
    </nav>
    <hr>
    <section>
      <%-body -%>
    </section>
    <script src="/public/scripts/fetch.js"></script>
    <%-scripts%>
  </body>
</html>

page.ejs

<% layout('layout') -%>
<link rel="stylesheet" href="/public/styles/designRights.css">
<p>Hello, World!</p>
<% script('/public/scripts/page.js') -%>