=head1 NAME Text::Xslate::Manual::Cookbook - How to cook Xslate templates =head1 DESCRIPTION The Xslate cookbook is a set of recipes showing Xslate features. =head1 RECIPES =head2 How to manage HTML forms 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. This section proposes two basic solutions: using FillInForm and HTML form builders. In both solutions, one should not use the C in templates, which easily makes B. Instead, application code should be responsible to call the C that C can export. =head3 Using FillInForm One solution to manage HTML forms is to use FillInForm modules with the block filter syntax. Example code using C: #!perl -w use strict; use Text::Xslate qw(html_builder); use HTML::FillInForm; # HTML::FillInForm::Lite is okay sub fillinform { my($q) = @_; my $fif = HTML::FillInForm->new(); return html_builder { my($html) = @_; return $fif->fill(\$html, $q); }; } my $tx = Text::Xslate->new( function => { fillinform => \&fillinform, }, ); my %vars = ( q => { foo => "" }, ); print $tx->render_string(<<'T', \%vars); FillInForm: : block form | fillinform($q) -> {
: } T Output: FillInForm:
Because L provides C function, it becomes more simple: use HTML::FillInForm::Lite qw(fillinform); my $tx = Text::Xslate->new( function => { fillinform => html_builder(\&fillinform) }, ); From 1.5018 on, C are supported for HTML builder modules like C. Just import HTML builder functions with C option. my $tx = Text::Xslate->new( html_builder_module => [ 'HTML::FillInForm::Lite' => [qw(fillinform)] ], ); See also L or L for details. =head3 Using HTML form builders Another solution to manage HTML forms is to use form builders. In such cases, all you have to do is to apply C to HTML parts. Here is a PSGI application that uses C: #!psgi use strict; use warnings; 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); # do mark_raw here, not in templates my $form = mark_raw($shakan->render()); $res->body( $tx->render_string(<<'T', { form => $form }) ); Building form

Form:
<: $form :>

T return $res->finalize(); } return \&app; Output: Building form

Form:

See also L for details. =head2 How to use Template Toolkit's WRAPPER feature in Kolon Use template cascading, which is a super-set of the C directive. F:
block content -> { }
F : cascade wrapper : override content -> { Hello, world! : } Output:
Hello, world!
=head3 Template cascading Xslate supports B