Text::Xslate::Cookbook - How to cook Xslate templates
The Xslate cookbook is a set of recipes showing Xslate features.
Managing HTML forms is an important issue on web applications. you'd better to use modules that manage HTML forms, rather than do something with templates by yourself. Basically there are two basic solutions in Xslate: use of FillInForm or HTML form builders.
In both solutions, one should not use the mark_raw filter in templates because of its security risks. Instead, application code should be responsible for it.
mark_raw
One solution to manage HTML forms is to use FillInForm modules with the block filter syntax and the raw filter.
raw
Example code using HTML::FillInForm:
HTML::FillInForm
#!perl -w use strict; use Text::Xslate qw(mark_raw); use HTML::FillInForm; sub fillinform { my($q) = @_; return sub { my($html) = @_; return mark_raw(HTML::FillInForm->fill(\$html, $q)); }; } my $tx = Text::Xslate->new( function => { fillinform => \&fillinform, }, ); my %vars = ( q => { foo => "<filled value>" }, ); print $tx->render_string(<<'T', \%vars); FillInForm: : block form | fillinform($q) -> { <form> <input type="text" name="foo" /> </form> : } T
Output:
FillInForm: <form> <input type="text" name="foo" value="<filled value>" /> </form>
See also HTML::FillInForm or HTML::FillInForm::Lite for details.
Another solution to manage HTML forms is to use form builders.
Example code using HTML::Shakan:
HTML::Shakan
#!psgi use strict; use Text::Xslate qw(mark_raw); use HTML::Shakan; use Plack::Request; my $tx = Text::Xslate->new(); sub app { my($env) = @_; my $req = Plack::Request->new($env); my $shakan = HTML::Shakan->new( request => $req, fields => [ TextField(name => 'name', label => 'Your name: ') ], ); my $res = $req->new_response(200); my $form = mark_raw($shakan->render()); $res->body( $tx->render_string(<<'T', { form => $form }) ); <!doctype html> <html> <head><title>Building form</title></head> <body> <form> <p> Form:<br /> <: $form :> </p> </body> </html> T return $res->finalize(); } return \&app;
<!doctype html> <html> <head><title>Building form</title></head> <body> <form> <p> Form:<br /> <label for="id_name">Your name</label> <input id="id_name" name="name" type="text" value="<Xslate>" /> </p> </body> </html>
See also HTML::Shakan for details.
Use URI::Escape or URI::Escape::XS.
URI::Escape
URI::Escape::XS
Code using URI::Escape:
#!perl -w use strict; use Text::Xslate; my $tx = Text::Xslate->new( module => ['URI::Escape'], ); print $tx->render_string(<<'T', { app_param => "foo & bar" }); <a href="http://example.com/app/<: $app_param | uri_escape_utf8 :>">something</a> T
<a href="http://example.com/app/foo%20%26%20bar">something</a>
See also URI::Escape or URI::Escape::XS.
Use template cascading, which is a super-set of the WRAPPER directive.
WRAPPER
wrapper.tx:
<div class="wrapper"> block content -> { } </div>
content.tx
: cascade wrapper : override content -> { Hello, world! : }
<div class="wrapper"> Hello, world! </div>
Text::Xslate
To install Text::Xslate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::Xslate
CPAN shell
perl -MCPAN -e shell install Text::Xslate
For more information on module installation, please visit the detailed CPAN module installation guide.