ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Express 템플릿 변수
    NodeJS 2019. 7. 4. 21:14

    서버에서 템플릿으로 변수를 보내는 두가지 방법이 있다

    1. 템플릿 전역 변수: 모든 템플릿에게 같은 변수를 준다

    2. 템플릿 로컬 변수 : 템플릿 마다 다르게 변수를 준다

     

    - 템플릿 전역 변수

    const localMiddleware = (req,res,next)=>{
    	res.locals.title = "타이틀";
    	next();
    }
    
    app.use(localMiddleware);

    res.locals를 이용하면 title 이라는 변수를 모든 템플릿에서 읽을 수 있다

     

    - 템플릿 로컬 변수

    const handleHome = (req,res)=>{
    	res.render("home", { title : "타이틀" });
    }

    res.render 메소드에 랜더링할 파일 다음으로 객체를 인자로 넣어주면

    해당 템플릿에만 객체에 들어있는 변수를 읽을 수 있다

    'NodeJS' 카테고리의 다른 글

    PUG 기능  (0) 2019.07.04
    Express X PUG  (0) 2019.07.04
    Express Router  (0) 2019.07.04
    Express 미들웨어  (0) 2019.07.04
    Express 라우팅  (0) 2019.07.04

    댓글

Designed by Tistory.