简体   繁体   中英

template() in Handlebar.js not accepting parameter

So, this is the code:

$('li.list').on('click', function(){
    console.log(this.id); 
    var temp = template(library) ; 
    console.log(temp); 
    $('.infotemplate').append(temp);
});

When I logged this.id, it gives me the correct id ie library. but when I pass that into template() function it does not parse the details of my object library. But when I explicitly pass the library as a parameter in function(), it gives the correct output. So, I want this dynamically, that when a user clicks on that id, the object of same name as that of id gets appended later in body.

$('li.list').on('click', function(){
    console.log(this.id); 
    var temp = template(this.id) ; 
    console.log(temp);
    $('.infotemplate').append(temp);
});

How do I solve this? Please help..

Instead of creating a variable for each item, create an object with all of them and use the id to access them from that variable..

What you are currently doing is passing the string library and not the variable with that name..

var items = {
   library : {icon:'..', title:'...',..},
   cityhall: {..},
   ...
} 

And use

$('li.list').on('click', function(){
    console.log(this.id); 
    var temp = template(items[this.id]) ; 
    console.log(temp);
    $('.infotemplate').append(temp);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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