[ejs] html 페이지 템플릿을 위한 ejs 모듈 예제

2016. 3. 8. 15:51·프레임워크/[JS] node.js

ejs 모듈은 html 템플릿 생성을 위한 모듈이다.


jsp 와 비슷한 형태를 하고 있으며, jsp 의 스크립틀릿 형태로 작성하면

자체 렌더러를 이용하여 html 형식으로 변환해 준다.


var ejs = require('ejs');
var fs = require('fs');

// file read
html_str = fs.readFileSync(__dirname + '/sample.ejs', 'utf-8');

// compile
template = ejs.compile(html_str);
console.log(template({ 'title' : 'compile' }));

// render
console.log(ejs.render(html_str, { 'title' : 'render' }));

compile과 render 메소드를 이용하여 변환하면 된다.


아래는 sample.ejs 파일의 내용이다.

<html>
	<head>
		<title><%= title %></title>
	</head>
	<body>
		<h1><%= title %></h1>
		<hr/>
		<% for(var i = 0; i < 10; i++) { %>
		    <h2>2 * <%= i %> = <%= 2 * i%><h2>
		<% } %>
	</body>
</html>

ejs에서 <%= 변수명 %> 는 System.out.print() 와 같고

<% %> 는 스크립트릿을 이용할 수 있다.

즉 해당 내용 안에서는 제어문이나, 반복문을 사용하면 된다.


변환된 결과는 다음과 같다.


<html>
	<head>
		<title>compile</title>
	</head>
	<body>
		<h1>compile</h1>
		<hr/>
		
		    <h2>2 * 0 = 0<h2>
		
		    <h2>2 * 1 = 2<h2>
		
		    <h2>2 * 2 = 4<h2>
		
		    <h2>2 * 3 = 6<h2>
		
		    <h2>2 * 4 = 8<h2>
		
		    <h2>2 * 5 = 10<h2>
		
		    <h2>2 * 6 = 12<h2>
		
		    <h2>2 * 7 = 14<h2>
		
		    <h2>2 * 8 = 16<h2>
		
		    <h2>2 * 9 = 18<h2>
		
	</body>
</html>


<html>
	<head>
		<title>render</title>
	</head>
	<body>
		<h1>render</h1>
		<hr/>
		
		    <h2>2 * 0 = 0<h2>
		
		    <h2>2 * 1 = 2<h2>
		
		    <h2>2 * 2 = 4<h2>
		
		    <h2>2 * 3 = 6<h2>
		
		    <h2>2 * 4 = 8<h2>
		
		    <h2>2 * 5 = 10<h2>
		
		    <h2>2 * 6 = 12<h2>
		
		    <h2>2 * 7 = 14<h2>
		
		    <h2>2 * 8 = 16<h2>
		
		    <h2>2 * 9 = 18<h2>
		
	</body>
</html>



https://www.npmjs.com/package/ejs




반응형
저작자표시 비영리 (새창열림)

'프레임워크 > [JS] node.js' 카테고리의 다른 글

[nodejs][express] express 설치 및 기본 프로젝트 생성하기  (0) 2016.03.30
[jade] node.js용 html 템플릿 엔진  (0) 2016.03.10
[에러] possible EventEmitter memory leak detected. 에러 처리  (0) 2016.02.18
node.js와 mysql 연동하기  (0) 2016.02.17
node.js의 LTS 버전과 Stable 버전의 차이  (1) 2016.01.27
'프레임워크/[JS] node.js' 카테고리의 다른 글
  • [nodejs][express] express 설치 및 기본 프로젝트 생성하기
  • [jade] node.js용 html 템플릿 엔진
  • [에러] possible EventEmitter memory leak detected. 에러 처리
  • node.js와 mysql 연동하기
hs_seo
hs_seo
Hello World!
    반응형
  • hs_seo
    개발자로 살아남기
    hs_seo
  • 전체
    오늘
    어제
    • 전체 (1140)
      • 개발자 (21)
        • 개발에 유의할 점 (0)
        • 면접 (5)
      • IT 소식 (5)
        • 업계 (1)
      • java (51)
        • 디자인패턴 (3)
        • apache-common (1)
      • 개념 (47)
        • 자료구조 (4)
        • 함수형사고 (8)
        • 디자인패턴 (1)
      • 데이터분석 (1)
      • python (67)
        • 코드조각 (12)
        • 라이브러리 (2)
      • 빅데이터 (418)
        • zookeeper (5)
        • hadoop (78)
        • hdfs (12)
        • hive (127)
        • hbase (16)
        • spark (40)
        • scala (4)
        • trino (3)
        • oozie (41)
        • Hue (9)
        • R (5)
        • sqoop (6)
        • flume (3)
        • elasticsearch (2)
        • airflow (16)
        • kafka (3)
        • kubernetes (10)
        • openstack (3)
        • flink (2)
        • redis (2)
      • 빅데이터 강좌 (2)
      • 알고리즘 (131)
        • 알고리즘 (1)
        • 백준 (61)
        • 정올 (41)
        • 더블릿 (5)
        • 프로그래머스 (1)
      • 프로그래밍 언어 (30)
        • go (4)
        • js (9)
        • .Net (6)
        • Jsp (1)
        • ansible (3)
        • terraform (6)
      • Tools (56)
        • docker (2)
        • macbook (6)
        • maven (3)
        • sublime (1)
      • 프레임워크 (25)
        • [JS] angularjs (2)
        • [JS] node.js (19)
        • [Java] spring (2)
        • Android (2)
      • 데이타베이스 (43)
        • SQLD (5)
        • Oracle (1)
        • MySQL (8)
        • ADsP (2)
      • 리눅스 (25)
        • Bash (61)
      • GCP (5)
      • AWS (34)
        • EC2 (2)
        • EMR (14)
      • 정보보안기사 (4)
        • 네트워크 (1)
      • 개인 (80)
        • 업무실수 (0)
        • 책 (9)
        • 교육 (3)
        • 여행 (17)
        • 영화 (12)
        • 음악 (2)
        • 피규어 (4)
        • 게임 (3)
        • 생각 (7)
        • 기타 (10)
        • 좋은글 (5)
        • 좋은 사이트 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

    • 빅데이터-하둡,하이브로 시작하기
    • 빅데이터-스칼라, 스파크로 시작하기
    • Kaggle에서 파이썬으로 데이터 분석 시작하기
    • 쉘스크립트 개발 시작하기
    • 개발자가 데이터 분석 준전문가 되기
    • 데브쿠마
  • 공지사항

  • 인기 글

  • 태그

    알고리즘
    SPARK
    Python
    build
    Tez
    bash
    오류
    yarn
    ubuntu
    HIVE
    error
    AWS
    airflow
    Hadoop
    하둡
    oozie
    emr
    hbase
    하이브
    mysql
    Linux
    정올
    백준
    java
    다이나믹
    HDFS
    k8s
    nodejs
    파이썬
    S3
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
hs_seo
[ejs] html 페이지 템플릿을 위한 ejs 모듈 예제
상단으로

티스토리툴바