Answer the question
In order to leave comments, you need to log in
Handlebars not working. What am I doing wrong?
There is this block:
<div class="hand">
<script id="objects" type="text/x-handlebars-template">
{{#each objects}}
<h3>{{title}}</h3>
<p>{{description}}
<a href="{{link}}">Ссылка</a>
</p>
{{/each}}
</script>
</div>
{
"objects": [
{
"title":"Заголовок",
"description":"Какой то текст",
"link":"https://toster.ru/question/new"
}
]
}
Смотрел несколько уроков. Почему некоторые используют ajax а некоторые нет?
Вот что я находил.
var data = {
name : 'John Doe'
},
var template = Handlebars.compile( $('#objects').html() );
$('.hand').append( template(data) );
var data = {
name : 'John Doe'
},
$(document).ready(function () {
var hand = $('.hand');
$.ajax({
url: 'data.json'
}).done(function(data){
var json = JSON.parse(data),
source = $("#objects").html(),
template = Handlebars.compile(source),
html = template(json);
$('.hand').append( template(data) );
})
});
Answer the question
In order to leave comments, you need to log in
Try to lay out a template from the resulting block.
<script id="objects" type="text/x-handlebars-template">
{{#each objects}}
<h3>{{title}}</h3>
<p>{{description}}
<a href="{{link}}">Ссылка</a>
</p>
{{/each}}
</script>
<div class="hand">
</div>
$(document).ready(function () {
var hand = $('.hand');
$.ajax({
url: 'data.json'
}).done(function(data){
var json = JSON.parse(data),
source = $("#objects").html(),
template = Handlebars.compile(source),
html = template(json);
$('.hand').append( html ); //-- было template(data)
})
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question