0

PHP内でhandlebars.jsを使用しようとしています。$_GET経由で渡された変数を取得して配列を作成し、それをhttp://my.service.url.comsearch.json?に追加しています。http_build_query($myarray, '', "&"); 私のURLを作成し、file_get_contentsを使用て「json」を取得します。結果を表示するために handlebars.js を使用しようとしています。Google Chrome コンソールで次のエラーが表示されます

「キャッチされていない TypeError: 未定義の cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:431 のメソッド 'match' を呼び出せません」

コンソールでエラーの詳細を見ると、これが表示されます

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 レックス 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 コンパイル cloud.github.com/downloads/wycats/handlebars.js/handlebars -1.0.rc.1.js:1842 (匿名関数) cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js:1851 (匿名関数) 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>

これは、「$("body").html(template(data));」の上のコンソール エラーで参照されている 39 行目です。

そして最後に、これが私が取得している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、住所:107 Carrollwood Dr、市:ラプラス、州:LA、url:http://www.carretarestaurant.com、緯度:30.44437、経度:-91.14768、ロゴ URL:http://url.com/imp/17 /logo/LG460880.JPG,enticerLine:\トラディショナル フレーバー メキシコ料理\,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:カーサ ガルシア メキシカン レストラン,電話:(504) 464-0354,住所: ,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、住所:8814 Veterans Memorial Bl、都市:メタリー、州:LA、緯度:30.005453、経度:-90.224346}、{listingId:115036、pageUrl:/page/LA/kenner/casa-tequila/17- 115036.html、名前:Casa Tequila、電話:(504) 443-5423、住所:3229 Williams Bl、市:ケナー、州:LA、緯度:30.014789、経度:-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、住所:4221 Williams Bl、都市:ケナー、州:LA、url:http://www.garcescubanrestaurant.com、緯度:30.033075、経度:-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:ニューオーリンズ,state: LA、緯度:30.022314、経度:-90.061329、enticerLine:予算重視の家族や旅行者が、アメリカを代表するタコス チェーンに乗り出します。},{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:ニューオーリンズ、状態:LA、緯度:29.919895、経度:-90.011082}、{listingId:672267490、pageUrl:/page/LA/new-orleans/taco-bell-new-orleans-4/cg-672267490.html、name:タコベル、電話:(504) 283-9006、住所:4603 Chef Menteur Hwy、都市:ニューオーリンズ、州:LA、緯度:30.006073、経度:-90.035487}、{listingId:733204300、pageUrl:/page/LA/new -orleans/taco-bell-orleans-4/cg-733204300.html、名前:Taco Bell、電話:(504) 246-6699、住所:6007 Bullard Ave、都市:ニューオーリンズ、州:LA、緯度:30.043732、経度:-89.958962}、{listingId:733204270、pageUrl:/page/LA/new-orleans/taco-bell-orleans/cg-733204270.html、名前:タコベル、電話:(504) 244-8761、住所: 5611 Read Blvd、都市:ニューオーリンズ、州:LA、緯度:30.030747、経度:-89。97091},{listingId:4431549,pageUrl:/page/LA/new-orleans/taco-bell-orleans-4/cg-4431549.html,名前:タコベル,電話:(504) 821-4458,住所: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,緯度:30.044695,経度:-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,緯度:29.902112,経度:-90.064841,enticerLine:予算重視の家族や旅行者が、アメリカを代表するタコス チェーンに乗り出しています。},{listingId:34159784,pageUrl:/page/LA/marrero/taco-bell-marrero-2/cg-34159784.html 、名前:タコベル、電話:(504) 341-5367、住所:5141 Lapalco Blvd、都市:マレロ、州:LA、緯度:29.874902、経度:-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:テリータウン,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、緯度:29.88825、経度:-90.056795}、{listingId:708288570、pageUrl:/page/LA/gretna/taco-bell-gretna/cg-708288570.html、名前:TACO BELL、電話:(504) 368- 3370、住所:62 WESTBANK EXPY # B、都市:GRETNA、州:LA、緯度:29.918798、経度:-90.044141}、{listingId:34183917、pageUrl:/page/LA/westwego/taco-bell-westwego/cg- 34183917.html、名前:タコベル、電話:(504) 341-6909、住所:1001 Westbank Expy、都市:Westwego、州:LA、緯度:29.898329、経度:-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,緯度:29.918975,経度:-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 ,緯度:29.930096,経度:-90.172423}]}{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:ウェストウィーゴ,州:ロサンゼルス,緯度:29.930096,経度:-90.172423}]}{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:ウェストウィーゴ,州:ロサンゼルス,緯度:29.930096,経度:-90.172423}]}

4

1 に答える 1

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

于 2013-01-11T16:27:54.440 に答える