handlebars lekcja 1
This commit is contained in:
1 parent
e42104c4fb
commit
527e0a34bb
1265 files changed
+213278
No files matched your search
@@ -0,0 +1,78 @@
|
||||
const express = require("express")
|
||||
const app = express()
|
||||
const PORT = 3000
|
||||
const path = require('path')
|
||||
|
||||
const { create } = require('express-handlebars');
|
||||
|
||||
const context = {
|
||||
subject: "09: użycie helperów",
|
||||
text: "Lorem ipsum dolor sit amet"
|
||||
}
|
||||
|
||||
const hbs = create({
|
||||
defaultLayout: 'main.hbs', // Domyślny layout: views/layouts/main.hbs
|
||||
extname: '.hbs', // rozszerzenie plików szablonów
|
||||
helpers: {
|
||||
short_title: function (title) {
|
||||
return title.substring(0, 10) + "...";
|
||||
},
|
||||
capitals: function (title) {
|
||||
let words = title.split(' ')
|
||||
let output = ''
|
||||
words.forEach(word => output += word[0].toUpperCase() + word.substring(1, word.length) + ' ')
|
||||
output = output.substring(0, output.length - 1)
|
||||
return output
|
||||
},
|
||||
dashes: function (title) {
|
||||
let str = title.replaceAll(" ", "")
|
||||
let output = ''
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
output += str[i] + '-'
|
||||
}
|
||||
output = output.substring(0, output.length - 1)
|
||||
return output
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
app.engine('.hbs', hbs.engine); // określenie silnika szablonów
|
||||
app.set('view engine', '.hbs');
|
||||
app.set('views', path.join(__dirname, 'views')); // ustalamy katalog views
|
||||
|
||||
app.get("/", function (req, res) {
|
||||
res.render('index09.hbs', context)
|
||||
})
|
||||
|
||||
app.get("/handle04", function (req, res) {
|
||||
let data = {
|
||||
subject: context.subject,
|
||||
books: []
|
||||
};
|
||||
|
||||
if (req.query.title != 'on' && req.query.author != 'on' && req.query.lang != 'on') {
|
||||
data.books.push({ value: "Musisz wybrać co najmniej jedno kryterium!" })
|
||||
} else {
|
||||
context.books.forEach(book => {
|
||||
let attributes = []
|
||||
if (req.query.title == 'on') {
|
||||
attributes.push(book.title)
|
||||
}
|
||||
if (req.query.author == 'on') {
|
||||
attributes.push(book.author)
|
||||
}
|
||||
if (req.query.lang == 'on') {
|
||||
attributes.push(book.lang)
|
||||
}
|
||||
data.books.push({ value: attributes.join(', ') })
|
||||
})
|
||||
}
|
||||
|
||||
res.render('index041.hbs', data);
|
||||
})
|
||||
|
||||
app.use(express.static("static"))
|
||||
|
||||
app.listen(PORT, function () {
|
||||
console.log("start serwera na porcie " + PORT)
|
||||
})
|
||||
Reference in new issue
Block a user