2013-01-11 76 views
0

我想在內部php中使用handlebars.js。我抓取通過傳遞的變量$ _GET構建一個數組,然後將其附加到http://my.service.url.comsearch.json ?. http_build_query($ myarray,'',「&」); buld我的網址,然後使用file_get_contents搶「json」。我正在嘗試使用handlebars.js來顯示結果。看着錯誤在控制檯的細節時,我reciving谷歌瀏覽器控制檯不能調用未定義的handlebars.js的方法「匹配」

"Uncaught TypeError: Cannot call method 'match' of undefined cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:431"

以下錯誤我看到這個

lexer.next cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:431 lex cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:468 lex cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:238 parse cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:251 Handlebars.parse cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:595 compile cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:1842 (anonymous function) cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:1851 (anonymous function) profile.php:39

這裏是我的模板,我使用

<script id="ul" type="text/x-handlebars-template"> 
    <ul> 
    {{#jsonResult}} 
     <li>{{name}}</li> 
    {{/jsonResult}} 
    </ul> 
    </script> 

這裏是我的腳本,我使用,企圖使用模板

</script> 
    <script type="text/javascript"> 
    var source = $("ul").html(); 
    var template = Handlebars.compile(source); 
    var json = $pJSON; 
    var data1 = JSON.stringify(json); 
    var data = JSON.parse(data1); 
    $("body").html(template(data)); 
</script> 

這是上面的控制檯錯誤中引用的第39行「$(」body「)。html(template(data));」

終於在這裏是我得到的JSON(以及採樣)

{totalCount:2,jsonResult:[{listingId:460880,pageUrl:/page/LA/laplace/la-carreta-of-laplace/17-460880.html,name:La Carreta Of LaPlace,phone:(985) 651-9991,address:107 Carrollwood Dr,city:Laplace,state:LA,url:http://www.carretarestaurant.com,latitude:30.44437,longitude:-91.14768,logoUrl:http://url.com/imp/17/logo/LG460880.JPG,enticerLine:\Traditional Flavor Mexican Cuisine\,photos:[{photoUrl:http://url.com/imp/17/photo/PH460880_1_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_2_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_3_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_4_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_5_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_6_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_7_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_8_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_9_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_11_S.jpg,photoDesc:},{photoUrl:http://url.com/imp/17/photo/PH460880_12_S.jpg,photoDesc:}]},{listingId:430765,pageUrl:/page/LA/new-orleans/casa-garcia-mexican-restaurant/17-430765.html,name:Casa Garcia Mexican Restaurant,phone:(504) 464-0354,address:,city:New Orleans,state:LA},{listingId:223993,pageUrl:/page/LA/metairie/casa-garcia-mexican-restaurant/17-223993.html,name:Casa Garcia Mexican Restaurant,phone:(504) 467-4071,address:8814 Veterans Memorial Bl,city:Metairie,state:LA,latitude:30.005453,longitude:-90.224346},{listingId:115036,pageUrl:/page/LA/kenner/casa-tequila/17-115036.html,name:Casa Tequila,phone:(504) 443-5423,address:3229 Williams Bl,city:Kenner,state:LA,latitude:30.014789,longitude:-90.239894,displayAd:http://url.com/imp/17/disp/DA115036_658760_1.JPG,displayAds:[{displayAd:http://url.com/imp/17/disp/DA115036_658760_1.JPG}]},{listingId:460184,pageUrl:/page/LA/kenner/garces-latin-american-restaurant/17-460184.html,name:Garces Latin American Restaurant,phone:(504) 305-5035,address:4221 Williams Bl,city:Kenner,state:LA,url:http://www.garcescubanrestaurant.com,latitude:30.033075,longitude:-90.238324},{listingId:34172784,pageUrl:/page/LA/new-orleans/taco-bell-new-orleans/cg-34172784.html,name:Taco Bell,phone:(504) 286-7901,address:6220 Elysian Fields Ave,city:New Orleans,state:LA,latitude:30.022314,longitude:-90.061329,enticerLine:Budget-minded families and travelers make a run for America's leading taco chain.},{listingId:34184616,pageUrl:/page/LA/new-orleans/taco-bell-new-orleans-5/cg-34184616.html,name:Taco Bell,phone:(504) 391-6930,address:4300 General Degaulle Dr,city:New Orleans,state:LA,latitude:29.919895,longitude:-90.011082},{listingId:672267490,pageUrl:/page/LA/new-orleans/taco-bell-new-orleans-4/cg-672267490.html,name:Taco Bell,phone:(504) 283-9006,address:4603 Chef Menteur Hwy,city:New Orleans,state:LA,latitude:30.006073,longitude:-90.035487},{listingId:733204300,pageUrl:/page/LA/new-orleans/taco-bell-orleans-4/cg-733204300.html,name:Taco Bell,phone:(504) 246-6699,address:6007 Bullard Ave,city:New Orleans,state:LA,latitude:30.043732,longitude:-89.958962},{listingId:733204270,pageUrl:/page/LA/new-orleans/taco-bell-orleans/cg-733204270.html,name:Taco Bell,phone:(504) 244-8761,address:5611 Read Blvd,city:New Orleans,state:LA,latitude:30.030747,longitude:-89.97091},{listingId:4431549,pageUrl:/page/LA/new-orleans/taco-bell-orleans-4/cg-4431549.html,name:Taco Bell,phone:(504) 821-4458,address:2639 Tulane Ave,city:New Orleans,state:LA,latitude:29.96146,longitude:-90.089724},{listingId:733204280,pageUrl:/page/LA/new-orleans/taco-bell-orleans-2/cg-733204280.html,name:Taco Bell,phone:(504) 240-6374,address:6009 Bullard Ave,city:New Orleans,state:LA,latitude:30.044695,longitude:-89.959174},{listingId:4415463,pageUrl:/page/LA/harvey/taco-bell-harvey/cg-4415463.html,name:Taco Bell,phone:(504) 362-0746,address:929 Manhattan Blvd,city:Harvey,state:LA,latitude:29.902112,longitude:-90.064841,enticerLine:Budget-minded families and travelers make a run for America's leading taco chain.},{listingId:34159784,pageUrl:/page/LA/marrero/taco-bell-marrero-2/cg-34159784.html,name:Taco Bell,phone:(504) 341-5367,address:5141 Lapalco Blvd,city:Marrero,state:LA,latitude:29.874902,longitude:-90.098789},{listingId:34141058,pageUrl:/page/LA/terrytown/taco-bell-terrytown/cg-34141058.html,name:Taco Bell,phone:(504) 394-7584,address:2640 Belle Chasse Hwy,city:Terrytown,state:LA,latitude:29.888259,longitude:-90.030674},{listingId:612584560,pageUrl:/page/LA/harvey/taco-bell-harvey-3/cg-612584560.html,name:Taco Bell,phone:(504) 374-0972,address:1740 Manhattan Blvd,city:Harvey,state:LA,latitude:29.88825,longitude:-90.056795},{listingId:708288570,pageUrl:/page/LA/gretna/taco-bell-gretna/cg-708288570.html,name:TACO BELL,phone:(504) 368-3370,address:62 WESTBANK EXPY # B,city:GRETNA,state:LA,latitude:29.918798,longitude:-90.044141},{listingId:34183917,pageUrl:/page/LA/westwego/taco-bell-westwego/cg-34183917.html,name:Taco Bell,phone:(504) 341-6909,address:1001 Westbank Expy,city:Westwego,state:LA,latitude:29.898329,longitude:-90.142391},{listingId:672876420,pageUrl:/page/LA/gretna/taco-party-gretna/cg-672876420.html,name:Taco Party,phone:(504) 312-3912,address:1115 van Trump St,city:Gretna,state:LA,latitude:29.918975,longitude:-90.045341},{listingId:647483470,pageUrl:/page/LA/westwego/speedy-taco-westwego/cg-647483470.html,name:Speedy Taco,phone:(504) 301-1524,address:1020 Bridge City Ave,city:Westwego,state:LA,latitude:29.930096,longitude:-90.172423}]}

回答

2
{{#jsonResult}} 
    <li>{{name}}</li> 
{{/jsonResult}} 

#jsonResult像你希望它不會在列表上進行迭代。您需要使用:

{{#each jsonResult}} 
    <li>{{name}}</li> 
{{/each}} 

請參閱http://handlebarsjs.com#iteration

編輯:

您需要的軌道上改變var source = $("ul").html();var source = $("#ul").html();

+0

你就在那裏 - 我居然忘了每個部分 - 然而糾正這個後,我似乎仍然有同樣的問題和錯誤 –

+0

請參閱編輯。您沒有爲Handlebars提供正確的模板。 – Anarchtica

+0

我已經做了這些更改已經消除了錯誤,但我似乎並沒有獲得數據到身體標記 - 我繼續併發布這個,所以你可以看到我的意思 - http://lstest.siteyp.com/ profile.php?listingId = 460880&where = new + orleans%2C + LA&what = taco –

相關問題