简体   繁体   English

把手不渲染模板

[英]Handlebars not rendering template

I have been on this issue for more than a couple of hours now and I have no idea why it isn't working. 我现在已经处理了这个问题超过几个小时,我不知道为什么它不起作用。 It's a simple {{#each}} block. 这是一个简单的{{#each}}块。 I will add I am new to Handlebars.js. 我将补充说我是Handlebars.js的新手。

I thought maybe there was something wrong with my [vanilla] javascript so I translated the code to JQuery, but that did not help. 我想也许我的[vanilla] javascript有问题所以我将代码翻译成了JQuery,但这没有用。

My Template: 我的模板:

<div class="eventListContainer" id="handlebarsEntryPoint">
</div>

<script id="eventList-template" type="text/x-handlebars-template">
    {{#each event}}
    <div class="evenEvent eventContent">
        <div class="eventThumbnails leftContent">
            EX1: {{this.city_name}}
        </div>
        <div class="eventInformation middleContent">
            EX2: {{this.id}}
        </div>
        <div class="eventGoogleMap rightContent">
            EX3: {{this.owner}}
        </div>
    </div>
    <hr>
    {{/each}}
</script>

Note : If I replace {{#each event}} with {{#each this}} then my template (as in only the HTML) is displayed without any data. 注意 :如果我用{{#each this}}替换{{#each event}},那么我的模板(仅在HTML中)显示时没有任何数据。

My Javascript: 我的Javascript:

eventList = response.events;
console.log(eventList);

var source = document.getElementById('eventList-template').innerHTML;
var template = Handlebars.compile(source);
var compiledHTML = template(eventList);
// compiledHTML = template({event: ['56','789','91011']});
var entryPoint = document.getElementById('handlebarsEntryPoint');
entryPoint.innerHTML = compiledHTML;
  • I tried removing the "this" keyword from {{this.city_name}} but that didn't do anything. 我尝试从{{this.city_name}}删除“this”关键字,但这没有做任何事情。
  • I tried a simpler example to just get the array working (the line that is commented out under My Javascript) but that also wasn't working. 我尝试了一个更简单的例子来让数组工作(在我的Javascript下注释掉的行),但这也没有用。
  • I have tripled checked to see if maybe I had incorrect logic to insert HTML. 我检查了三倍,看看是否有错误的逻辑来插入HTML。
  • I have looked at countless examples which I tried modeling but still nothing. 我看过无数的例子,我尝试过建模,但仍然没有。

console.log(eventList) gives me: console.log(eventList)给了我:

Object {event: Array[5]}
  event: Array[5]
    0: Object
    1: Object
    2: Object
    3: Object
    4: Object
    length: 5
    __proto__: Array[0]
  __proto__: Object

I am not looking for a solution in JQuery. 我不是在寻找JQuery的解决方案。 Thanks in advance :D 提前致谢

Edit 1 - Contents of an Object in the event Array 编辑1 - 事件数组中对象的内容

https://sdjs.slack.com/files/jacome09/F2BNNPDRN/events_object.js

This should do it. 这应该做到这一点。

 var source = document.getElementById('eventList-template').innerHTML; var template = Handlebars.compile(source); var compiledHTML = template({event: [{city_name: 'KC',id:'56',owner:'MO'},{city_name: 'SF',id:'1000',owner:'CA'}]}); var entryPoint = document.getElementById('handlebarsEntryPoint'); entryPoint.innerHTML = compiledHTML; 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script> <div class="eventListContainer" id="handlebarsEntryPoint"> </div> <script id="eventList-template" type="text/x-handlebars-template"> {{#each event as |ev|}} <div class="evenEvent eventContent"> <div class="eventThumbnails leftContent"> EX1: {{ev.city_name}} </div> <div class="eventInformation middleContent"> EX2: {{ev.id}} </div> <div class="eventGoogleMap rightContent"> EX3: {{ev.owner}} </div> </div> <hr> {{/each}} </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM