Waft::JSON - JSON extensions for Waft
package MyWebApp; use Waft with => '::JSON'; # for Waft 0.9905 or later
or
package MyWebApp; use base qw( Waft::JSON Waft );
Waft テンプレートのアウトプットプロセスにおいて、JSON への変換機能を提供する。
Waft では、テンプレートファイル内の "<%" と "%>" で囲まれた部分はスクリプト レットとして処理され、Waft 標準では、"<%word=" などのエスケープ補助機能が 提供されている。
本モジュールは、新たに "<%json=" で指定できる、JSON への変換機能を提供する。 "<%json=" と "%>" で囲まれた部分は、JSON モジュールにより、JSON へ変換後、 出力される。
データがハッシュリファレンスの場合、
<% my $data_hashref = { foo => 1, bar => 2 }; %> <%json= $data_hashref %> {"bar":2,"foo":1}
と出力され、データが配列リファレンスの場合、
<% my $data_arrayref = [1, 2, 3, 4, 5]; %> <%json= $data_arrayref %> [1,2,3,4,5]
と出力される。 また、混在の場合も、
<% my $data_hashref = { foo => 1, bar => [2, 3, 4] }; %> <%json= $data_hashref %> {"bar":[2,3,4],"foo":1}
のように出力される。
本機能は、Ajax リクエストへのレスポンス処理を想定して作成したものである。 JavaScript では、JSON はそのままオブジェクトとして解釈されるため、 Ajax のコールバック処理時のレスポンスデータとして扱いやすくなる。
Waft::JS や Waft::jQuery と組み合わせて使用すると、Ajax 開発の補助 となるはずである。
Shingo Murata, <singo@silk.plala.or.jp>
Copyright (C) 2009 by Shingo Murata
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
=back without =over
To install Waft::JSON, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Waft::JSON
CPAN shell
perl -MCPAN -e shell install Waft::JSON
For more information on module installation, please visit the detailed CPAN module installation guide.