Dancer2

 view release on metacpan or  search on metacpan

t/integration/template/rendering.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
use Test::Fatal qw<exception>;
use Plack::Test;
use HTTP::Request::Common;
use Path::Tiny ();

use Dancer2::Template::TemplateToolkit;

# Template rendering: whether the layout is applied, which layout, and what
# tokens a view can count on.
#
# The layout decision is made in Role::Template::apply_layout
# (Dancer2/Core/Role/Template.pm:169-172) and distinguishes three cases that are
# easy to collapse into two by accident:
#
#   layout key absent from options  -> use the configured layout
#   layout key present and true     -> use *that* layout
#   layout key present and false    -> no layout at all
#
# Template::Toolkit is used rather than Template::Tiny because it is stricter
# about undefined tokens and is what most applications actually run.

my $VIEWS;
BEGIN {
    $VIEWS = Path::Tiny->tempdir;
    $VIEWS->child('layouts')->mkpath;

    $VIEWS->child('body.tt')->spew_utf8('BODY');
    $VIEWS->child('empty.tt')->spew_utf8('');
    $VIEWS->child( 'layouts', 'main.tt' )->spew_utf8('MAIN{[% content %]}');
    $VIEWS->child( 'layouts', 'alt.tt' )->spew_utf8('ALT{[% content %]}');

    # Every default token, reported so a missing one is visible rather than
    # silently rendering as empty.
    $VIEWS->child('tokens.tt')->spew_utf8(
        join ' ',
        'settings=[% settings.some_setting %]',
        'param=[% params.q %]',
        'var=[% vars.myvar %]',
        'session=[% session.who %]',
        'request=[% request.path %]',
        'perl=[% perl_version %]',
        'dancer=[% dancer_version %]',
    );
}

{
    package TemplateApp;
    use Dancer2;
    set logger       => 'null';
    set views        => $VIEWS->stringify;
    set template     => 'template_toolkit';
    set layout       => 'main';
    set session      => 'Simple';
    set some_setting => 'SETTING-VALUE';

    get '/configured' => sub { template 'body' };
    get '/suppressed' => sub { template 'body', {}, { layout => 0 } };
    get '/undef'      => sub { template 'body', {}, { layout => undef } };
    get '/named'      => sub { template 'body', {}, { layout => 'alt' } };
    get '/empty'      => sub { template 'empty', {}, { layout => 0 } };



( run in 0.747 second using v1.01-cache-2.11-cpan-54e63673c56 )