One way of returning it is by writing whole HTML documents on the controller and send out.
Ex)export const see = (req, res) => { return res.send(`<!DOCTYPE><HTML><head></head><body></body></HTML>`); };
Pug is a high performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers. link to pug
npm i pug
//server.js
app.set("view engin", "pug");
views
folder inside src
foldername.pug
file inside of views
folder as HTML file.//Example
doctype html
html(lang="en")
head
title= pageTitle
script(type='text/javascript').
if (foo) bar(1 + 5)
body
h1 Pug - node template engine
#container.col
if youAreUsingPug
p You are amazing
else
p Get on it!
p.
Pug is a terse and simple templating language with a
strong focus on performance and powerful features.
res.render("home");
Error: Failed to lookup view "home" in views directory "/Users/followthesnake/Desktop/youtubeclone/views"
console.log(process.cwd());
(cwd means Current Working Directory/Users/nameOfTheUser/Desktop/youtubeclone
```javascript
//server.js
app.set("views", process.cwd() + "/src/views");
```
partials are used for repetitive components like footer. Since you don't want to type the same thing for every single pages, you separate the component and just include the filename to the pages you need to.
include partials/filename.pug
to the pug file you want to attach the footer.Inheritance is like a mold. You can make files with same structure over and over again, easy and fast.Just like you use same variables over and over again.
//base.pug
doctype html
html(lang="kr")
head
title Home | YoutubeClone
body
Home
include partials/footer.pug
This structure is going to be inherited to the files you want by putting
//home.pug
extends base.pug
Block is a window. When you need same structure but different contents inside of the each page, you can use block.
//base.pug
doctype html
html(lang="kr")
head
title Home | YoutubeClone
body
block content //This is the block.
include partials/footer.pug
β¬ Extended to home.pug
//home.pug
extends base.pug
block content
h1 Home
//base.pug
doctype html
html(lang="kr")
head
title #{pageTitle} | YoutubeClone //This is the variable
body
block content
include partials/footer.pug
res.render("home", { pageTitle: "Home" });
${variableName}
tagName=variableName
// both are same
First one with ${} is used when the variable is mixed with other text. and =variableName is used when only one variable is there to use.
//In a controller with a dummyUser
const fakeUser = {
username: "Koodal",
loggedIn: false,
}
export const trending = (req, res) => res.render("home", { pageTitle: "Home", fakeUser });
We had to write
fakeUser: fakeUser
in an older version of JS, but after ES6, now we can write it like above.
body
header
if fakeUser.loggedIn
small Hello #{fakeUser.username}!
nav
ul
if fakeUser.loggedIn
li
a(href="/logout") Log out
else
li
a(href="/login") login
We can put conditionals inside of pug. (Feels like I'm making conditionals with HTML right away π€©)
// controller
export const trending = (req, res) => {
const videos = [1,2,3,4,5,6];
res.render("home", { pageTitle: "Home", videos });
};
//home.pug
each variableName in arrayname
li=variableName
else something you can write
variableName in
each variableName in arrayname
can be anything as long as the arrayname is right.
Works like a partials but receives data.
Premade HTML block.
Mixins are used to make same format of a component.
For example, this project is cloning Youtube. And When you go inside the youtube, you see thumbnames(videos) all over in every single pages.
Video thumbnails are there when you first go to Youtube, while you're watching a video, there's a sidebar shows more videos related to the video you're watching or related to you're interest.
To make a website like youtube, you need some sort of a frame which can iterate data. And that is mixins
//Example array inside a controller
const videos = [
{
title: "Planet of Apes",
rating: 4.3,
comments: 23,
createdAt: "2 minutes ago",
views: 220,
id: 1,
},
{
title: "Planet of Apes 2",
rating: 4.3,
comments: 23,
createdAt: "2 minutes ago",
views: 220,
id: 1,
},
{
title: "Planet of Apes 3",
rating: 4.3,
comments: 23,
createdAt: "2 minutes ago",
views: 220,
id: 1,
},
mixin video(info)
div
h4=info.title
ul
li #{info.rating}/5
li #{info.comments} comments
li Posted: #{info.createdAt}
li #{info.views} views total
//home.pug
extends base
include mixins/video
block content
each video in videos
+video(video)
video
fromvideos
gets iterated in the+video(video)
. and each video info interates and shows on the browser.
More info about Pug: Pug Documentation