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,56 @@
|
||||
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.use(express.urlencoded({
|
||||
extended: true
|
||||
}));
|
||||
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('index09a.hbs', context)
|
||||
})
|
||||
|
||||
|
||||
|
||||
app.use(express.static("static"))
|
||||
|
||||
app.listen(PORT, function () {
|
||||
console.log("start serwera na porcie " + PORT)
|
||||
})
|
||||
Reference in new issue
Block a user