Dancer
view release on metacpan or search on metacpan
lib/Dancer/Cookbook.pod view on Meta::CPAN
<% IF session.username %>
<p>You're logged in as <% session.username %>
<% END %>
It's currently <% vars.time %>
For a full list of the tokens automatically added to your template
(like C<session>, C<request> and C<vars>, refer to
L<Dancer::Template::Abstract>).
=head3 Layouts
A layout is a special view, located in the 'layouts' directory (inside the views
directory) which must have a token named 'content'. That token marks the place
to render the action view. This lets you define a global layout for your
actions, and have each individual view contain only the specific content. This
is a good thing to avoid lots of needless duplication of HTML :)
Here is an example of a layout: C<views/layouts/main.tt> :
<html>
<head>...</head>
<body>
<div id="header">
...
</div>
<div id="content">
<% content %>
</div>
</body>
</html>
You can tell your app which layout to use with C<layout: name> in the config
file, or within your code:
set layout => 'main';
You can control which layout to use (or whether to use a layout at all) for a
specific request without altering the layout setting by passing an options
hashref as the third param to the template keyword:
template 'index.tt', {}, { layout => undef };
If your application is not mounted under root (B</>), you can use a
before_template_render hook instead of hardcoding the path to your
application for your css, images and javascript:
hook 'before_template_render' => sub {
my $tokens = shift;
$tokens->{uri_base} = request->base->path;
};
Then in your layout, modify your css inclusion as follows:
<link rel="stylesheet" href="<% uri_base %>/css/style.css" />
From now on, you can mount your application wherever you want, without
any further modification of the css inclusion
=head3 template and unicode
If you use L<Plack> and have some unicode problem with your Dancer application,
don't forget to check if you have set your template engine to use unicode, and
set the default charset to UTF-8. So, if you are using template toolkit, your
config.yml will look like this:
charset: UTF-8
engines:
template_toolkit:
ENCODING: utf8
=head3 TT's WRAPPER directive in Dancer (META variables, SETs)
Dancer already provides a WRAPPER-like ability, which we call a "layout". The
reason we do not use TT's WRAPPER (which also makes it incompatible with Dancer) is
because not all template systems support it. Actually, most don't.
However, you might want to use it, and be able to define META variables and
regular L<Template::Toolkit> variables.
These few steps will get you there:
=over 4
=item * Disable the layout in Dancer
You can do this by simply commenting (or removing) the C<layout> configuration
in the F<config.yml> file.
=item * Use Template Toolkit template engine
Change the configuration of the template to Template Toolkit:
# in config.yml
template: "template_toolkit"
=item * Tell the Template Toolkit engine who's your wrapper
# in config.yml
# ...
engines:
template_toolkit:
WRAPPER: layouts/main.tt
=back
Done! Everything will work fine out of the box, including variables and META
variables.
=head1 SETTING THE STAGE: CONFIGURATION AND LOGGING
=head2 Configuration and environments
Configuring a Dancer application can be done in many ways. The easiest one (and
maybe the dirtiest) is to put all your settings statements at the top of
your script, before calling the dance() method.
Other ways are possible. You can define all your settings in the file
`appdir/config.yml'. For this, you must have installed the YAML module, and of
course, write the config file in YAML.
That's better than the first option, but it's still not perfect as you can't
switch easily from one environment to another without rewriting the config.yml
( run in 1.104 second using v1.01-cache-2.11-cpan-39bf76dae61 )