ejs에서 include 사용하기

HR·2022년 6월 9일
0
post-thumbnail

ejs 파일들을 사용하면서, 다음과 같이 코드들을 여러 파일로 분리해서 만드는 경우가 많다.

위의 간단한 예제에서는 index의 head와 body부분을 따로 파일로 생성해서, index 파일에 include 해서 사용하려고 한다.

기존의 방식에서는 include가 아래와 같은 방법으로 가능했다.

index.ejs

<html>
    <head>
        <% include ./head.ejs %>
    </head>
    <body>
        <% include ./body.ejs %>
    </body>
</html>

하지만 ejs가 3.x 버전으로 올라가면서 include 방식이 아래와 같이 변경되었다.

index.ejs

<html>
    <head>
        <%-include('head.ejs')%>
    </head>
    <body>
        <%-include('body.ejs')%>
    </body>
</html>

head.ejs

<title>
    testing include!
</title>

body.ejs

<h1>
    hello world!
</h1>

0개의 댓글