javascript - Mustache Dynamically set variable -


i need in using javascript variable set dynamically mustache template variable. here problem. json looks this

jsondata = { "recipemodules" : {     "enchiladas" : [         {             "title" : "chicken enchiladas",             "thumbnailurl" : "http:www.google.com/image",             "recipelink" : "location recipe",             "credit" : "some credit"         },         {             "title" : "chicken enchiladas",             "thumbnailurl" : "http:www.google.com/image",             "recipelink" : "location recipe",             "credit" : "some credit"         }     ],     "roastchicken" : [         {             "title" : "chicken enchiladas",             "thumbnailurl" : "http:www.google.com/image",             "recipelink" : "location recipe",             "credit" : "some credit"         },         {             "title" : "chicken enchiladas",             "thumbnailurl" : "http:www.google.com/image",             "recipelink" : "location recipe",             "credit" : "some credit"         }     ],     "salmon" : [         {             "title" : "salmon ",             "thumbnailurl" : "http:www.google.com/image",             "recipelink" : "location recipe",             "credit" : "some credit"         },         {             "title" : "chicken enchiladas",             "thumbnailurl" : "http:www.google.com/image",             "recipelink" : "location recipe",             "credit" : "some credit"         }      ] } 

};

then doing next is... going webpage...grabing url , based on url, set variable.

var thecurrenturl = window.location.href;  var finaljsonobject = "";    switch(thecurrenturl) {  	case "www.google.com/something1":  		finaljsonobject = "enchiladas";  		break;  	case "www.google.com/something2":  		finaljsonobject = "roastchicken";  		break;  	case "www.google.com/something3":  		finaljsonobject = "salmon";  		break;  	default:  }

finally... here mushtache template looks like

// create template  template = '{{#enchiladas}}<div class="mri-container"><img src="{{thumbnailurl}}" /></div>{{/enchiladas}}';   // parse template   mustache.parse(template);   // render template   rendered = mustache.render(template, jsondata.recipemodules);   			     // inject template dom   carousel.html(rendered);

now runs fine when use {{#enchiladas}}{{/enchiladas}} ... not want. want able use variable name instead of enchiladas.

if @ switch statement... returns finaljsonobject string. want template

template = '{{#finaljsonobject}}<div class="mri-container"><img src="{{thumbnailurl}}" /></div>{{/finaljsonobject}}'; 

but not work. mustache template using jsondata.recipemodules data. therefore jsondata.recipemodules.enchiladas work... want dynamically set "enchiladas" set on switch statement. want use dynamically set variable in template. want use jsondata.recipemodules.finaljsonobject based on page... data want.

please me! thank much.

konstantin

i think looking wrong direction here. pass variable render instead of jsondata.recipemodules if want last template work.
illustrate that:

// jsondata definition before var thecurrenturl = window.location.href; var templatedata = {};  switch(thecurrenturl) {     case "www.google.com/something1":         templatedata.finaljsonobject = jsondata.recipemodules["enchiladas"];         break;     case "www.google.com/something2":         templatedata.finaljsonobject = jsondata.recipemodules["roastchicken"];         break;     case "www.google.com/something3":         templatedata.finaljsonobject = jsondata.recipemodules["salmon"];         break;     default: } // create template template = '{{#finaljsonobject}}<div class="mri-container"><img src="{{thumbnailurl}}" /></div>{{/finaljsonobject}}';  // parse template  mustache.parse(template);  // render template  rendered = mustache.render(template, templatedata);   // inject template dom  carousel.html(rendered); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -