Answer the question
In order to leave comments, you need to log in
Multiple html gulp + jade generation?
Hello. You need to make a lot of the same type of html pages, with different titles/descriptions/links.
The problem is that there is some kind of pagination on the inner pages (forward/backward). The problem is that now there are 10 such pages, they must be sorted alphabetically and can be added later. If you need to add such a page between existing ones, then we will get the problem that the links to the next and previous ones do not match. And on this it is necessary to generate it somehow. I use Gulp, Jade (Pug) but is it possible to somehow do this with their help, well, or alternative solutions? (you need to get pages like: partner_name.html, patner_newname.html ...)
For example, create a json with a list of these pages and parameters, and create a jade template and pass parameters to this template and compile it. But I don't understand how to do it.
Anticipating questions:
- no, you can't use js template engines on the client
- no, you can't use php What you
need is page generation (plain html).
Answer the question
In order to leave comments, you need to log in
We install Node.js, npm, pug
https://www.npmjs.com/package/pug
We read the documentation: https://pugjs.org/api/reference.html Using
the examples from the documentation, we write a script that will take your template and json and render as many pages as needed.
Can someone help, wrote an example:
Node.js
var jade = require('jade'),
fs = require('fs'),
path = require('path'),
IterateObject = require("iterate-object");
fs.readFile('template.jade', 'utf8', function (err, data) {
if (err) throw err;
var page = JSON.parse(fs.readFileSync('test.json', 'utf8'));
var fn = jade.compile(data, {
pretty: true
});
var next,prev;
IterateObject(page, function (value, i, arr) {
if(arr[i + 1]) {
next = arr[i + 1].url;
} else{
next = '';
}
if(arr[i - 1]) {
prev = arr[i - 1].url;
} else{
prev = '';
}
arr[i].next = next;
arr[i].prev = prev;
var html = fn(arr[i]);
fs.writeFile("my_"+arr[i].url+".html", html, 'utf8');
});
});
[
{
"name": "",
"url": "",
"text": "<div>bla bla</div>",
"logo": "",
"facebook": "",
"linkedin": "",
"twitter": "",
"www": ""
}]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question