Answer the question
In order to leave comments, you need to log in
How to use template correctly?
Good afternoon!
There is a site on pure HTML on 20 pages.
I want to put the header, footer and some other blocks into separate files and connect them.
But at the same time, you need to change the titles, description and other text.
For this, I decided to use the Handlebars template engine.
How can I use it correctly, namely how to pass data to variables for their subsequent output?
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
Answer the question
In order to leave comments, you need to log in
in the same place in the dock everything is written
handlebarsjs.com
The most common example of a very simple templating engine, you may not need handlebars
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
<script>
template(document.querySelector('.entry'), {
title: 'Lorem ipsum dolor sit amet.',
body: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eos, optio.'
});
function template(node, data) {
node.innerHTML = tag(node.innerHTML, data);
}
function tag(input, data) {
for (var key in data) {
input = input.replace(new RegExp('{{'+ key +'}}', 'g'), data[key]);
}
return input;
}
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question