简体   繁体   English

无法调用未定义的 handlebars.js 的方法“匹配”

[英]Cannot call method 'match' of undefined handlebars.js

I am trying to use handlebars.js inside PHP.我正在尝试在 PHP 中使用handlebars.js I am grabbing variables passed via $_GET building an array and then appending it to http://my.service.example.com/search.json?. http_build_query($myarray, '', "&");我正在获取通过$_GET传递的变量构建一个数组,然后将其附加到http://my.service.example.com/search.json?. http_build_query($myarray, '', "&"); http://my.service.example.com/search.json?. http_build_query($myarray, '', "&"); to buld my URL and then using file_get_contents to grab the JSON.构建我的 URL,然后使用file_get_contents获取 JSON。 I am trying to use handlebars.js to display the results.我正在尝试使用handlebars.js来显示结果。 and I am receiving the following error in the Google chrome console我在 Google chrome 控制台中收到以下错误

"Uncaught TypeError: Cannot call method 'match' of undefined cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:431" “未捕获的类型错误:无法调用未定义 cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:431 的方法‘匹配’”

when looking at the details of the error in the console I see this在控制台中查看错误的详细信息时,我看到了这个

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

Here is my template I am using这是我正在使用的模板

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

here is my script I am using in an attempt to use the template这是我试图使用模板的脚本

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));

this is the line 39 referenced in the console error above $("body").html(template(data));这是$("body").html(template(data));上方控制台错误中引用的第 39 行

and finally here is the JSON I am getting (well a sample)最后这是我得到的 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}]}
{{#jsonResult}}
  <li>{{name}}</li>
{{/jsonResult}}

#jsonResult doesn't iterate over the list like you want it to. #jsonResult不会像您希望的那样遍历列表。 You need to use:你需要使用:

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

See http://handlebarsjs.com#iteration .请参阅http://handlebarsjs.com#iteration

You also need to change var source = $("ul").html();您还需要更改var source = $("ul").html(); to var source = $("#ul").html();var source = $("#ul").html();

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

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