だいたい191日前に更新最終更新日時: 2010-03-02 (火) 10:25:02 191日前
現在の位置
FrontPage > Perl > フレームワーク(MVC) > Sledge > Tenjin
perlのテンプレートエンジン"Tenjin"をSledgeから利用するためのモジュールです
Tenjin のインストールなどについては こちら をご覧下さい。
Sledge::Template::Tenjin †
package Sledge::Template::Tenjin; use strict; use warnings; use vars qw($VERSION); $VERSION = 0.01; use base qw(Sledge::Template); use Fcntl qw/:flock/; use File::Basename; use File::Path; use Tenjin; use Encode; use Sledge::Exceptions; sub import { my $class = shift; my $pkg = caller(0); no strict 'refs'; *{"$pkg\::create_template"} = sub { my($self, $file) = @_; return $class->new($file, $self); }; } sub new { my($class, $file, $page) = @_; bless { _options => { filename => $file, PATH => [ $page->create_config->tmpl_path, '.' ], }, _params => { config => $page->create_config, #r => $page->r, #session => $page->session, }, }, $class; } sub add_associate { Sledge::Exception::UnimplementedMethod->throw } sub associate_namespace { Sledge::Exception::UnimplementedMethod->throw } our $_tenjin = ''; sub output { my $self = shift; my $config = $self->{'_options'}; my $input = delete $config->{'filename'}; if ($config->{'COMPILE_DIR'}) { $Tenjin::cache_dir = $config->{'COMPILE_DIR'}; } my $tenjin = $_tenjin ||= Tenjin->new({ 'postfix' => $config->{'POSTFIX'} ? $config->{'POSTFIX'} : '.phtml', 'preprocess' => $config->{'PREPROCESS'} ? $config->{'PREPROCESS'} : 0, 'path' => $config->{'PATH'}, }); unless (-e $input) { Sledge::Exception::TemplateNotFound->throw( 'No template file detected. Check your template path.', ); } my $output = $tenjin->render($input,$self->{'_params'}); if( !Encode::is_utf8($output) && $Tenjin::ENCODING eq 'utf8') { Encode::_utf8_on($output); } return $output; } ## ---------------------------- ## ## for Tenjin.pm ## ---------------------------- ## { no warnings qw/redefine/; ## define cache-dir function *Tenjin::cachename = sub { my ($self, $filename) = @_; return $Tenjin::cache_dir ? $Tenjin::cache_dir.$filename . '.cache' : $filename . '.cache'; }; *Tenjin::store_cachefile = sub { my ($self, $cachename, $template) = @_; mkpath(dirname($cachename)) unless -f $cachename; my $cache = $template->{script}; if (defined $template->{args}) { my $args = $template->{args}; $cache = "\#\@ARGS " . join(',', @$args) . "\n" . $cache; } if($Tenjin::ENCODING eq 'utf8' && !Encode::is_utf8($cache)) { Encode::_utf8_on($cache); } $template->_write_file($cachename, $cache, 1); }; ## add utf8-flag *Tenjin::Template::_read_file = sub { my ($self, $filename, $lock_required) = @_; open(IN, $filename) or die("Tenjin::Template: Can't open $filename for reading: $!"); binmode(IN); flock(IN, LOCK_SH) if $lock_required; read(IN, my $content, -s $filename); close(IN); ## add if($Tenjin::ENCODING eq 'utf8' && !Encode::is_utf8($content)) { Encode::_utf8_on($content); } return $content; }; ## ---------------------------- ## ## for Tenjin/Template.pm ## ---------------------------- ## *Tenjin::Template::_write_file = sub { my ($self, $filename, $content, $lock_required) = @_; open(OUT, ">$filename") or die("Tenjin::Template: \"Can't open $filename for writing: $!\""); binmode(OUT); flock(OUT, LOCK_EX) if $lock_required; ## add if($Tenjin::ENCODING eq 'utf8' && Encode::is_utf8($content)) { $content = Encode::encode('utf-8',$content); } print OUT $content; close(OUT); }; } ## no warnings qw/redefine/; 1;
lib/Proj/Pages.pm での create_template の定義例 †
{ no warnings qw/redefine/; sub create_template { my($self, $file) = @_; my $tmpl = Sledge::Template::Tenjin->new($file, $self); $tmpl->add_option(PATH => $self->create_config->tmpl_path.'/includes'); $tmpl->add_option(PREPROCESS => 1); $tmpl->add_option(TMPL_EXTENTION => '.html'); $tmpl->add_option(COMPILE_DIR => config->cache_dir.'/tenjin'); $tmpl->param(r => $self->r); $tmpl->param(session => $self->session); $tmpl->param(now => Time::Piece->new() ); return $tmpl; } ## Filter *Tenjin::Context::autolink = \&Proj::Utils::autolink; } ## no warnings.
view/index.html 例 †
- エスケープ
<h2>[= '">' =]</h2>
- アンエスケープ
<h2>[== '">' =]</h2>
<?pl include('includes/hd.html') ?> <h1>hello, World</h1> <h2>こんにちは!</h2> <h2>[== $config->service_title ==]</h2> aaaaaaaaaaaaaaaaaa: <h2>[= $env_remote_request_uri ==]</h2> <h2>[= '">' ==]</h2> aaaaaaa aaaaaaa <?pl my $var = 'ほげほげ http://ow.ly/1pp2hG ほげほげ'; ?> <h3>[== autolink($var) ==]</h3> <h3>[= $fdh ==]</h3> <ul> <?pl foreach my $map_area (@$map_area_entries) { ?> <li>[== $map_area->name ==] <ul> <?pl foreach my $map_pref (@{$map_area->map_pref_entries}) { ?> <li><a href="[== $map_pref->id ==]">[== $map_pref->name ==]</a></li> <?pl } ## foreach my $map_pref (@$map_pref_entries) { ?> </ul> </li> <?pl } # foreach my $map_area ($map_area_entries) { ?> </ul> <ul> <?pl foreach my $map_pref (@$map_pref_entries) { ?> <li><a href="[== $map_pref->id ==]">[== $map_pref->name ==]</a></li> <?pl } # foreach my $map_area ($map_area_entries) { ?> </ul> <h3>[== autolink($var) ==]</h3> <?pl include('includes/ft.html') ?>
- 関連ページ
ツールボックス
メニュー
最新の20件
最新の20件
2010-09-05
2010-09-01
2010-07-31
2010-07-27
2010-07-26
2010-04-23
2010-04-07
2010-04-04
2010-04-03
2010-03-31
2010-03-20
2010-03-19
2010-03-02
- Perl/フレームワーク(MVC)/Sledge/Tenjin

