На основе ответа https://stackoverflow.com/a/31837264/4360308 Я реализовал эту функцию с помощью Nodejs (+ express + cheerio) следующим образом:
HTML (index.html)
JS
function includeComponents($) {
$('.include').each(function () {
var file = 'view/html/component/' + $(this).data('include') + '.html';
var dataComp = fs.readFileSync(file);
var htmlComp = dataComp.toString();
if ($(this).data('method') == "replace") {
$(this).replaceWith(htmlComp);
} else if ($(this).data('method') == "append") {
$(this).append(htmlComp);
}
})
}
function foo(){
fs.readFile('./view/html/index.html', function (err, data) {
if (err) throw err;
var html = data.toString();
var $ = cheerio.load(html);
includeComponents($);
...
}
}
append -> содержит содержимое в div
replace -> заменяет div
, вы можете легко добавить более поведение по той же схеме