2012-10-08 49 views
2

有沒有辦法使用Mojolicious渲染引擎在網頁請求之外渲染模板?如何在Mojolicious的Web請求之外呈現模板?

+1

你應該接受鐸的解決方案,因爲他做了一些努力,它幫助你 –

+0

謝謝你的提示,我很樂意。一個人如何「接受解決方案」?我沒有看到任何按鈕或讓我這樣做。 – freyfogle

+0

啊我剛剛意識到我必須點擊複選標記。抱歉非常不直觀。 – freyfogle

回答

6

use Mojolicious::Renderer; 

my $renderer = Mojolicious::Renderer->new; 

push @{renderer->paths}, '/path/to/your/templates'; 

my $template = $renderer->get_data_template({ 
    template  => 'foo/bar', 
    format   => 'html', 
    handler  => 'epl' 
}); 
+1

謝謝,我知道它必須簡單,但渲染文檔只是不顯示語法。 – freyfogle

0

這是一個更全面(多最新的)解決方案,允許充分利用所有可用的完整Mojolicious堆棧渲染插件。

use Mojolicious; 

unless (@ARGV) { 
    die "$0: <template base name> [key value pairs]\n"; 
} 

my $app = Mojolicious->new(secrets => ['ignored']); 
my $c = $app->build_controller; 
my $r = $app->renderer; 

push @{$r->paths}, './templates';   # directory containing templates 
$c->app->log->level('fatal'); 


my $template = shift;      # template base name e.g. 'index' which looks up ./templates/index.html.ep 
$c->stash(shift, shift) while @ARGV >= 2; # add extra parameters into cache 
my $out = $c->render_to_string($template); 
print $out if $out; 

exit 0; 
相關問題