我需要幫助將動態設置的javascript變量設置爲我的小鬍子模板變量。這是問題。我的JSON看起來像這樣小鬍子動態設置變量
jsonData = {
"recipemodules" : {
"enchiladas" : [
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
},
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
}
],
"roastchicken" : [
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
},
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
}
],
"salmon" : [
{
"title" : "salmon ",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
},
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
}
]
}
};
接下來我接下來要做的是......進入網頁......抓取URL並基於該URL設置一個變量。
var theCurrentURL = window.location.href;
var finalJSONobject = "";
switch(theCurrentURL) {
\t case "www.google.com/something1":
\t \t finalJSONobject = "enchiladas";
\t \t break;
\t case "www.google.com/something2":
\t \t finalJSONobject = "roastchicken";
\t \t break;
\t case "www.google.com/something3":
\t \t finalJSONobject = "salmon";
\t \t break;
\t default:
}
最後......這是我的mushtache模板是什麼樣子
// 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);
\t \t \t
// inject template to DOM
carousel.html(rendered);
現在它運行良好,當我使用{{} #enchiladas } {{/ enchiladas}} ...但這不是我想要的。我希望能夠使用變量名稱而不是enchiladas。
如果你看看我的switch語句...它會返回finalJSONobject作爲字符串。我想讓我的模板看起來像
template = '{{#finalJSONobject}}<div class="mri-container"><img src="{{thumbnailurl}}" /></div>{{/finalJSONobject}}';
但這不起作用。小鬍子模板使用jsonData.recipemodules作爲其數據。因此,jsonData.recipemodules.enchiladas將工作...但我想動態設置「enchiladas」,這是設置在我的switch語句。我想在模板中使用我的動態設置變量。我想使用jsonData.recipemodules.finalJSONobject,以便基於頁面...我看我想要的數據。
請幫幫我!非常感謝。
Konstantin
Matias ...非常感謝你。我現在可以擁抱你!它的工作現在。謝謝你,謝謝你! – scriptonian