MT ã¢ããªã±ã¼ã·ã§ã³ã Sinatra like ã«è¨è¿°ã§ãã MT::App::Lite ä½ã£ã
Movable Type ã«ã¯ãMT::App ã¨ãã Web ã¢ããªã±ã¼ã·ã§ã³ãè¨è¿°ããããã®åºåºã¯ã©ã¹ãããã¾ãããçµæ§æ £ããå¿ è¦ãªã®ã¨çããããã¨ããã£ã¦ããã£ã¨æ軽㫠Sinatra like ã«ã¢ããªã±ã¼ã·ã§ã³ãè¨è¿°ã§ããããã¬ã¼ã ã¯ã¼ã¯çãªãã®ã欲ãããªã¼ã¨å¸¸ã æã£ã¦ãã¾ããã
ã¨ãããããããããããããã¨ã¯æ®ã£ã¦ãã¾ãããåãã¨ããã¾ã§ã§ããã®ã§ãç´¹ä»ãã¾ãã
ç¾ç¶ã§ã¯ãPSGI ã§ã®ã¿åä½ãã¾ãã
åå
æ¦è¦
以ä¸ã®ãããªæãã§MTã¢ããªã±ã¼ã·ã§ã³ãè¨è¿°ãã¾ãã
package MyLiteApp; use strict; use MT::App::Lite; setup Renderer => 'Xslate'; get '/' => sub { my $app = shift; $app->render('index.tt', { blog => MT->model('blog')->load(1), entries => [MT->model('entry')->load({blog_id => 1})], }); }; get '/entry/:id' => sub { my $app = shift; $app->render('entry.tt', { blog => MT->model('blog')->load(1), entry => [MT->model('entry')->load($app->param('id'))], }); }; 1; __DATA__ @@ index.tt <!doctype html> <html> <head> <meta charset="utf-8"> <title><: $blog.name :></title> </head> <body> <ul> : for $entries -> $entry { <li><a href="<: $entry.permalink :>"><: $entry.title :></a></li> : } </ul> </body> </html> @@ entry.tt <!doctype html> <html> <head> <meta charset="utf-8"> <title><: $entry.title :> | <: $blog.name :></title> </head> <body> <h1><: $entry.title :></h1> <div> <: $entry.text | mark_raw :> </div> </body> </html>
1ãã¡ã¤ã«å®çµåã§ããï¼æ£ç¢ºã«ã¯ãã©ã°ã¤ã³ã® config.yaml ãå¿
è¦ï¼
å¤åããã©ã¼ã ã¨ãä½ãã®ã«ä¾¿å©ã ã¨æãã¾ãã
ä»æ§
MT ã¢ããªã±ã¼ã·ã§ã³ã®ãã³ãã©å ã§ãRouter::Simple::Sinatraish ã§ã¨ã¯ã¹ãã¼ãããããget ã post ãªã©ã®é¢æ°ã使ã£ã¦ãã«ã¼ããè¨è¿°ãã¾ãã
ãã®ãã¡ã¤ã«ä¸ã® __END__ ã»ã¯ã·ã§ã³ã«ããã³ãã¬ã¼ããæ¸ãã¾ãã
__END__ ã»ã¯ã·ã§ã³ã«è¨è¿°ãããã³ãã¬ã¼ãã¯ãData::Section::Simple ã«ãã£ã¦ãMT::App::Lite ã«æ¸¡ããã¾ãã
ãã³ãã¬ã¼ãã¯ãText::Xslate ãæ¨æºã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³ã«ãã¦ãã¾ããç³ã訳ç¨åº¦ã« MTML ç¨ã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³ãç¨æãã¾ããããããã§ãããã使ãå¿ è¦ã¯ãªãæ°ããã¾ãã
MT::App::Lite::Renderer::Foo ã¨ããå½¢ã§ãrender ã¡ã½ãã㨠render_string ã¡ã½ãããç¨æããã°ãä»»æã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³ãå©ç¨ãããã¨ã¯å¯è½ã§ãã
ã¾ããMT::App::Lite ã use ããã¨ãèªåçã« MT::App ãç¶æ¿ãããã¨ã«ãªãã®ã§ãMT::App ã£ã½ãæ¸ãæ¹ãã§ããã®ããããã¾ããã
åºæ¬çã«ã¯ãMT::App ã« Web ã¢ããªã±ã¼ã·ã§ã³ãè¨è¿°ããããã®æ©è½ã¯ã²ã¨éãæã£ã¦ããã®ã§ã便å©ã£ã½ãã¢ã¸ã¥ã¼ã«ãç³ä»ããããããªå®è£ ã§ãã
MT ã¢ããªã±ã¼ã·ã§ã³ã®ãã©ã°ã¤ã³ãä½æãã
ãã®ã¢ã¸ã¥ã¼ã«ã使ã£ããMTã¢ããªã±ã¼ã·ã§ã³ã®ãã©ã°ã¤ã³ã®ä½ãæ¹ã§ãã
æå°éã® config.yaml ã¨ããã³ãã©ãç¨æãã¾ãã
config.yaml
name: MyLiteApp id: myliteapp applications: lite_app: handler: MyLiteApp script: sub { 'app' } cgi_path: sub { '/' }
MyLiteApp (handler)
package MyLiteApp; use strict; use MT::App::Lite; setup Renderer => 'Xslate'; get '/' => sub { my $app = shift; $app->render('index.tt', { blog => MT->model('blog')->load(1), entries => [MT->model('entry')->load({blog_id => 1})], }); }; get '/entry/:id' => sub { my $app = shift; $app->render('entry.tt', { blog => MT->model('blog')->load(1), entry => [MT->model('entry')->load($app->param('id'))], }); }; 1; __DATA__ @@ index.tt <!doctype html> <html> <head> <meta charset="utf-8"> <title><: $blog.name :></title> </head> <body> <ul> : for $entries -> $entry { <li><a href="<: $entry.permalink :>"><: $entry.title :></a></li> : } </ul> </body> </html> @@ entry.tt <!doctype html> <html> <head> <meta charset="utf-8"> <title><: $entry.title :> | <: $blog.name :></title> </head> <body> <h1><: $entry.title :></h1> <div> <: $entry.text | mark_raw :> </div> </body> </html>
ããã§ãhttp://yourdomain/app/ ããhttp://yourdomain/app/entry/:id ã«ã¢ã¯ã»ã¹ããã¨ãã³ãã¬ã¼ãã®å 容ãæç»ããã¾ãã
ã¾ã¨ã
æ¬å½ã¯ãMovable Type Advent Calendar 2013 ã®èªåæ å½æ¥ã®ãã¿ã¨ãã¦ä½ããã¨èãã¦ãããã§ããã©ãå®è£ ããæããªããä»æ¥ã«è³ãã¾ãã
便å©ãªæ°ãããã®ã§ãã¡ã³ããã¦ãããã¨æãã¾ããã¾ã ããããã¨å®è£ ããããã¨ããå®è£ ãã¹ããã¨ãããã¾ãã
ç¾æç¹ã§ã¯ã¡ãã£ã¨ãã Viewer ããã©ã¼ã ãä½ãã®ã«ã¯ä¾¿å©ãªæ°ããã¦ãã¾ãã