CGI::Application::Plugin::Mason - HTML::Mason plugin for CGI::Application
1.01
package YourApp; use strict; use base qw(CGI::Application); use CGI::Application::Plugin::Stash; # require! use CGI::Application::Plugin::Mason; # cgiapp_init sub cgiapp_init { my $self = shift; $self->interp_config( comp_root => "/path/to/root", data_dir => "/tmp/mason" ); } # runmode sub start { my $self = shift; # Catalyst like $self->stash->{name} = "kurt"; $self->stash->{age} = 27; # template path $self->stash->{template} = "/start.mason"; return $self->interp_exec; } # start.mason <%args> $name $age </%args> <html> <head> <% # $c is YourApp object %> <title><% $c->get_current_runmode %></title> </head> <body> name : <% $name | h %><br> age : <% $age | h %> </body> </html>
CGI::Application::Plugin::Mason is Plug-in that offers HTML::Mason template engine.
Initialize HTML::Mason::Interp method.
Option:
comp_root : HTML::Mason root dir(default: Cwd::getcwd value) data_dir : HTML::Mason cache and object file directory(default: /tmp/mason) template_extension : template extension(default: .mason)
Example:
sub cgiapp_init { my $self = shift; $self->interp_config( comp_root => "/path/to/comp_root", data_dir => "/tmp/mason" ); # When pass other HTML::Mason option $self->interp_config( comp_root => "/path/to/comp_root", default_escape_flags => [ "h" ], autohandler_name => "autohandler", ); }
HTML::Mason::Interp object wrapper
# HTML::Mason::Interp#set_escape $self->interp->set_escape( uc => sub { ${$_[0]} =~ tr/a-z/A-Z/ } ); # HTML::Mason::Interp#comp_root my $comp_root = $self->interp->comp_root;
Return HTML::Mason::Interp#exec result.
The specification of the template file
# file name $self->stash->{template} = "/template.mason" # file handle open my $fh, "/path/to/template.mason" or croak("can not open file"); $self->stash->{template} = $fh; # scalarref $self->stash->{template} = \q{<%args>$name</%args> my name is <% $name %>};
default template name is /package_name/runmode_method_name . ${template_extension}
# ex1 package MyApp; sub start { my $self = shift; do something... # The file passing used at this time is /MyApp/start.mason return $self->interp_exec; } # ex2 package My::App; sub start { my $self = shift; do something... # The file passing used at this time is /My/App/start.mason return $self->interp_exec; }
Specification of variable allocated in template
# ex1 sub start { my $self = shift; # stash method setting $self->stash->{name} = "kurt"; $self->stash->{age} = 27; return $self->interp_exec; } # ex2 sub start { my $self = shift; # interp_exec param setting return $self->interp_exec( name => "kurt", age => 27 ); }
Trigger method before interp_exec. the argument is $temlate, and $arg.
$template : $self->{template} value $args : $self->{stash} or $self->interp_exec args hashref
sub interp_pre_exec { my($self, $template, $args) = @_; $args->{newval} = "interp_pre_exec setting value!"; } # or $self->add_callback("interp_pre_exec", sub { my($self, $template, $args) = @_; $args->{newval} = "interp_pre_exec setting value!"; });
Trigger method after interp_exec. the argument is $bodyref.
$bodyref : content value scalarref
sub interp_post_exec { my($self, $bodyref) = @_; ${$bodyref} = encode("shiftjis", decode("utf8", ${$bodyref})); } # or $self->add_callback("interp_post_exec", sub { my($self, $bodyref) = @_; ${$bodyref} = encode("shiftjis", decode("utf8", ${$bodyref})); });
html escape
Get default template path.
CGI::Application CGI::Application::Plugin::Stash HTML::Mason
Akira Horimoto
This library is free software. You can redistribute it and/or modify it under the same terms as perl itself.
Copyright (C) 2007 Akira Horimoto
To install CGI::Application::Plugin::Mason, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CGI::Application::Plugin::Mason
CPAN shell
perl -MCPAN -e shell install CGI::Application::Plugin::Mason
For more information on module installation, please visit the detailed CPAN module installation guide.