mixin:
๋ค๋ฅธ ์ ๋ณด + ๊ฐ์ ํ์์ ๊ฐ์ง ๊ฒ๋ค์ ์บก์ํํ๋ ์ปจ์ ์ ์ผ์ปซ๋๋ค.
ex) DB records
db -> web๊น์ง์ ํ๋ฆ
1. db.js
2. videoController.js
3. home.pug
4. videoBlock.pug
1. db.js
: db.js์์ ๊ฐ์ฒด'enlistedVideo' exportํ์.
export const enlistedVideo = { ... }
2. videoController.js
: controller์์ 'home'ํ์ด์ง๋ก ๋๋๋ง ํ ๋ ๊ฐ์ฒด'videos'๋ฅผ ํฌํ๋ฆฟ์ผ๋ก ์ ๋ฌํ์.
import { enlistedVideo } from "../db";
export const home = (req, res) => res.render("home", { pageTitle: "Home", videos: enlistedVideo });
โ
3. home.pug
: home์ด videoBlock.pug์๊ฒ {item.์์1, item.์์2 ...}, {item.์์1, item.์์2 ...}๋ฅผ ๋๊ฒจ์ค๋ค.
ํ์: + mixin์ด๋ฆ( ์ ๋ฌ ํ ๋ณ์ )
extends layouts/main.pug
include mixins/videoBlock.pug
block content
.videos
each item in videos // videos์ ์๋ ์์๋ค์ ํ๋ํ๋ ๋นผ์
+videoBlock({ // mixin์ผ๋ก ๋๊ฒจ์ค
title:item.title,
views:item.views,
videoFile:item.videoFile
})
4. mixins/videoBlock.pug
: ์ ๋ฌ ๋ฐ์ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ง๊ณ ์๋ ์์ฑ๋ค ์ค title, views๋ฑ์ ๋นผ์, mixin์ด ์ฐ๋ ์์ฑ ์ด๋ฆ์ผ๋ก ๋๊ฒจ์ฃผ๋ฉด ๋๋ค.
ํ์: mixin name(ํ๋ผ๋ฏธํฐ = { })
mixin videoBlock(video = {})
.videoBlock
video.videoBlcok__thumnail(src=video.videoFile, controls=true)
h4.videoBlcok__title=video.title
h4.videoBlcok__views=video.views
๋ฐ์ํ
'๐ Back > ๐งฉ Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์นํฉ(Webpack) ์ฐ๋ ์ด์ / ์ฌ์ฉ ๋ฐฉ๋ฒ (0) | 2021.02.13 |
---|---|
์ฌ์ฉ์ ์ธ์ฆ ๋ฏธ๋ค์จ์ด: PassportJS (0) | 2021.02.12 |
View Engine <PUG> <res. locals> (0) | 2021.01.25 |
[Express ๋ฏธ๋ค์จ์ด ์ฝ๋] app.use(), app.get(), Router() (0) | 2021.01.25 |
[Express ๋ฏธ๋ค์จ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ] morgan, helmet, cookie-parser, body-parser (0) | 2021.01.25 |