Jemplate 㧠JavaScript ã§ããã¸ãã¯ã¨ãã¥ã¼ãåé¢ãã
JSON ã Template-Toolkit ã§å±éãã Jemplate ã¨ããè¨äºãæ¸ãããã§ãããJemplate ã使ãã¨ä½ããããã£ã¦ã®ãããå°ã詳ããæ¸ãã¦ã¿ã¾ãã
Jemplate 㯠TT 㧠JavaScript ä¸ã® JSON ãå±éã§ãããã§ãããããã ãèãã¦ãããããã¨ãããã§æ®æ®µãµã¼ãã¼ãµã¤ãã§ãã£ã¦ããã³ãã¬ã¼ãã®å±éãã¯ã©ã¤ã¢ã³ããµã¤ãã«æã£ã¦è¡ãã¦è² è·ãã¯ã©ã¤ã¢ã³ãã«ç§»ã£ã¦ã¦ãã¼ãã£ã¦ããã®ã使ãã©ããã®ããã«ãæãã¡ããã¾ããã©ããããããªããæ¤ç´¢ã¨ã³ã¸ã³ã«å¼ã£ããããªããªã£ããã¨ããã¢ããªã±ã¼ã·ã§ã³ã®ä½¿ãåæãæªããªã£ããã¨ãè²ã å¼å®³ãããã¾ãã
ãããããªãã¦ãJemplate 㯠JavaScript ã®ããã®ãã³ãã¬ã¼ãã¨ãã¦ä½¿ãã¾ãã
試ãã« Catalyst ã§ç°¡åãªã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ã¿ã¾ãããã¡ãã£ã¨åããã¦ããç°å¢ããªãã®ã§ã½ã¼ã¹ã ãã®è§£èª¬ã«ãªã£ã¡ããã¾ããã
DNS lookup ãã¤ã³ã¯ãªã¡ã³ã¿ã«ã«ãã£ã¦ãã¾ããã¨ãããã®ãé¡æã«ãã¾ããã
ãããªæãã®ãã¾ã㯠Jemplate ã使ããã«ãHTML ã®ã½ã¼ã¹ã¯ä»¥ä¸ã
<html> <head> <script type="text/javascript" src="/static/js/prototype.js"></script> <script type="text/javascript" src="/static/js/resolver.js"></script> </head> <body> <h1>Ajax DNS Resolver</h1> <form id="lookup_handler" onsubmit="return false"> <input type="text" name="query" /> <input type="submit" value="lookup" /> </form> <div id="result"></div> </body> </html>
JavaScript ã® resolver.js ã¨ããã®ãããã®ã¢ããªã±ã¼ã·ã§ã³ã®ããã³ãã¨ã³ãå¦çã®ä¸»ãªç®æã§ãããµã¼ãã¼ãµã¤ãããåå解決ã®çµæã JSON ã§åãåã£ã¦ãid="result"
ãªè¦ç´ ã«çµæãå±éããã¨ãããã¨ããã£ã¦ã¾ããprototype.js ã使ã£ã¦ã¾ãã
resolver.js ã®ä¸èº«ã¯ä»¥ä¸ã§ãã
function lookup() { new Ajax.Request('/resolv', { parameters : Form.serialize($('lookup_handler')), onComplete : function (request) { var output = ''; var records = eval(request.responseText); if (records.length) { output = "<h2>" + records.length + " records are found.</h2>"; output = output + '<ul>'; for (var i = 0; i < records.length; i++) output += '<li>' + records[i].address + '</li>'; output = output + '</ul>'; } $('result').innerHTML = output; } }); return false; } Event.observe(window, 'load', installHandler); function installHandler() { Event.observe($('lookup_handler').query, 'keyup', lookup); }
Ajax.Request
㧠/resolv ã¨ãã URL ãå©ãã¨ãJSON ãè¿ã£ã¦ããã®ã§ããã eval ãã¦ãã¼ã¿æ§é ã JavaScript ä¸ã§åæ§ç¯ããå¾ããã¡ããã¡ãã¨ããã£ã¦ãã¾ãã
åèã¾ã§ã«ã/resolv ãå©ããã¨ãã«å®è¡ãããã³ã³ããã¼ã© Resolv.pm ã®ã½ã¼ã¹ã¯ä»¥ä¸ã§ãã
package MyApp::Controller::Resolv; use strict; use warnings; use base 'Catalyst::Controller'; use Net::DNS; use JSON::Syck; sub default : Private { my ( $self, $c ) = @_; my $query = $c->req->param('query'); my $resolver = Net::DNS::Resolver->new; my $result = $resolver->search($query); my @data; if ($result) { for my $ans ($result->answer) { my $record = {}; for my $attr (qw(name type address class rdatastr ttl)) { $record->{$attr} = $ans->$attr; } push @data, $record; } } $c->res->content_type('text/javascript'); $c->res->output(JSON::Syck::Dump(?@data)); } 1;
Net::DNS 㧠DNS æ¤ç´¢ãã¦ããã®çµæãããã¼ã¿æ§é ãä½ã£ã¦ãããã JSON::Syck 㧠JSON ã«ãã¦ã¯ãåºãã¨ãããã¨ããã¦ã¾ãã
ã§ã¾ããããã§ææã®ã¢ããªã±ã¼ã·ã§ã³ã«ãªãã¯ãªããã§ããã©ãå ã® resolver.js ã®ä¸èº«ãè¦ãã¨ã
var records = eval(request.responseText); if (records.length) { output = "<h2>" + records.length + " records are found.</h2>"; output = output + '<ul>'; for (var i = 0; i < records.length; i++) output += '<li>' + records[i].address + '</li>'; output = output + '</ul>'; }
ã¨ããé¨åãããªã¨ããã¨ããæãã«ãªã£ã¦ãã¾ãã¾ããXML ãããªã㦠JSON ã使ã£ã¦ã¦ãDOM ã¿ãããªãããããããã³ã¼ãããªãã®ã§ã¾ã ãã·ã¨ã¯è¨ããçµã¿ç«ã¦ã HTML ãè¤éã«ãªãã°ãªãã»ã©ãããããã¼ããªæãã«ãªã£ã¡ããã¾ãã
ããããæã«ãã JavaScript ãã³ãã¬ã¼ãã®åºçªãªããã§ãprototype.js ã®æ°ãããã¼ã¸ã§ã³ã«ã¯ããããæ©è½ãä»ãã¦ããããæ¢åã®ãã®ã§ãããã¤ãããããã®ã¯ãããããããJemplate ã使ãã¨æ®æ®µãªãã¿ã®æ·±ã TT ã®ã·ã³ã¿ãã¯ã¹ã§ãããæ±ãã¡ããã¾ããã¨ããããããã¨ã«ãªãã¾ãã
Jemplate ã使ãããã« resolver.js ã®ä¸ãæ¸ãæ¿ããã¨ã
function lookup() { new Ajax.Request('/resolv', { parameters : Form.serialize($('lookup_handler')), onComplete : function (request) { var records = eval(request.responseText); if (records.length) { $('result').innerHTML = Jemplate.process( 'result.tt', { records : records } ); } } }); return false; }
ãããªæãã«ãªã£ã¦ãHTML ã追ãåºããã¨ãã§ãã¾ããé©å½ãªã¨ããã« result.tt ã¨ãããã¡ã¤ã«ãä½ã£ã¦ã
<h2>[% records.size %] records are found.</h2> <ul> [% FOREACH rec IN records -%] <li>[% rec.address %]</li> [% END -%] </ul>
ã¨ãã風ã«æ¸ããããã jemplate ã§ã³ã³ãã¤ã«ããã
$ jemplate --compile jemplates/*.tt > static/js/jemplate01.js
ããã§ãããã HTML ããã¤ã³ã¯ã«ã¼ããã¾ãã¨ã
<html> <head> <script type="text/javascript" src="/static/js/prototype.js"></script> <script type="text/javascript" src="/static/js/Jemplate.js"></script> <script type="text/javascript" src="/static/js/jemplate01.js"></script> <script type="text/javascript" src="/static/js/resolver.js"></script> </head> ...
ããã§åããå¤ããã«ãJavaScript ã®ä¸èº«ã綺éºã«ãããã¨ãã§ãã¦ä¿å®æ§ããããã¾ããè¦ããã«ãJemplate 㯠JSON 㨠TT ã使ã£ã JavaScript ãã³ãã¬ã¼ããã®ãã®ã£ã¦ããããã§ã
ä»å¾ Jemplate ã®æ©è½ãæ¡å¼µããã¦ãã£ã¦ãä¾ãã° TT ãã©ã°ã¤ã³ã¿ãããªãã®ãå¦çã§ããããã«ãªã£ããããã¨ãJavaScript ãã³ãã¬ã¼ã以ä¸ã®æ©è½ãããããæå¾ ã§ãã¦ãããªãã¡ããã¯ã¼å¤¢ãã²ããããããã¨ãªããããã¨ãããã¨ã§ãã
ã¡ãªã¿ã«ãJemplate ã§ç½®ãæããçã¯åºåãã¡ããã¨åºã¾ããã§ããï½ ä¸å¿ FOREACH ãã£ã¬ã¯ãã£ã㧠JSON ãåãã¨ããã¾ã§ã¯ããã¦ããã§ãããrec.address
ã®å¤ã undefined ã ã£ãããTT ã® Virtual Methods ãã¾ã å®è¡ã§ããªãã®ã§ records.size
ã§å¤ãå¾ãããªãã£ããã¨ãããã®è¾ºã¯ããããã®éçºååã«æå¾
ã
ã¨ããã§ãã®ãµã³ãã«ã¢ããªã±ã¼ã·ã§ã³ã§ãããä»ææ«(2/23)ã«çºå£²ããã WEB+DB PRESS Vol.31 ã«æ¸ããè¨äºã§ä½¿ã£ããã®ãã¡ããã£ã¨æ¹é ãã¦ã¾ããè¨äºã®å 容ã¯ã¨ããã¾ãã¨ããããã« Jemplate ã®ãã¨ã«ã¯è§¦ãã¦ã¾ããããprototype.js ã®è©±ãä¸å¿ã« Class-style OO ã¨ã Ajax ã¨ãããã¨ã¡ãã£ã¨ JSON ã¨ããããã話ãæ¸ãããã®ã«ãªã£ã¦ã¾ãã