Skip to content

Using Template Toolkit with Squatting

October 21, 2008

Been playing with Squatting recently. Its a Perl Web microframework inspired by Camping. For someone like me who is still converting some old CGI apps to Catalyst then this looks like an interesting alternative.

I use Template Toolkit quite a bit so here’s a boilerplate for using TT with Squatting…..

use strict;
use warnings;

{
    package App;
    use base 'Squatting';

    BEGIN {
        our %CONFIG = (
            TT => {
                config   => { INCLUDE_PATH => '.' },
                postfix  => '.tt',
                site_tit => 'TT loves Squatting!',
            }
        );

    }
}

{
    package App::Controllers;
    use Squatting ':controllers';

    our @C = (

        C(
            Home => [ '/' ],
            get  => sub {
                my ( $self )  = @_;
                my $v         = $self->v;
                $v->{ tit }   = 'welcome to my homepage';
                $self->render( 'home' );
            },
        ),
        
        C(
            Page => [ '/path/to/page' ],
            get  => sub {
                my ( $self ) = @_;
                my $v        = $self->v;
                $v->{ tit  } = 'here is another page!';
                $v->{ list } = [ 'one', 'two', 'three' ];
                $self->render( 'page' );
            },
        ),

    );
}

{
    package App::Views;
    use Squatting ':views';
    use Template;

    our @V = (

        V(
            'html',
            tt => Template->new( $App::CONFIG{ TT }->{ config } ),

            layout => sub {
                my ( $self, $v, $body ) = @_;
                my $tt = $self->{ tt };
                $v->{ site_tit } = $App::CONFIG{ TT }->{ site_tit };
                $v->{ body     } = $body;
                
                my $output;
                $tt->process( 'layout'. $App::CONFIG{ TT }->{ postfix }, $v, \$output)
                    or return view_error( $tt->error );
                return $output;
            },

            _ => sub {
                my ( $self, $v ) = @_;
                my $tt = $self->{ tt };
                $v->{ R } = \&R;
                
                my $output;
                $tt->process( $self->{template} . $App::CONFIG{ TT }->{ postfix }, $v, \$output) 
                    or return view_error( $tt->error );
                return $output;
            }

        ),
    );
    
    sub view_error {
        my ( $error ) = shift;
        warn $error;
        return "<pre>" . $error . "</pre>\n";   # really need to HTML encode $error here
    }
}

1;

And here are the templates….

layout.tt

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>[% site_tit %]</title>
</head>
<body>
[% body %]
</body>
</html>

home.tt

<h1>Hello.. [% tit %]</h1>

<a href="[% R('Page') %]">Have a look here</a>

page.tt

<h1>Hello again... [% tit %]</h1>

<p>$v->{list}...</p>

<ul>
    [% FOREACH item IN list %]
    <li>[% item %]</li>
    [% END %]
</ul>

<a href="[% R('Home') %]">back to home</a>

Stash variables in Squatting (ie. variables that can used between controller & the view/template) can be set/get by $self->v
NB. “v” is for variables and shouldn’t be confused in anyway with “view” ;-(

Using “layout[.tt]” method/template is optional in Squatting (and Camping) so u can drop this and it will just render the view/template only. The code uses “layout” thus home.tt & page.tt renders as [% body %] via layout.tt

R() returns the route used by the specified controller ( for eg. R(‘Page’) returns “/path/to/page” ). This is quite a handy subroutine to export to TT templates!

So see it working just name above program “App.pm” and put in same directory as the template files and just do squatting App and then point your browser to http://localhost:4234/

Hopefully more on Squatting soon. So if u fancy a bit of fun web programming then give Squatting a go!

Advertisement
3 Comments leave one →
  1. November 1, 2008 7:04 pm

    In Squatting when you render a view prefixed with a “_” ( for eg. $self->render( '_page' ); ) then this will call the template without using the layout view.

    Unfortunately my TT code above doesn’t cope with the fact that $self->{template} would be prefixed with the “_” ;-(

    Below is a fixed App::Views….

    package App::Views;
    use Squatting ':views';
    use Template;
    
    our @V = (
    
        V(
            'html',
            tt => Template->new( $App::CONFIG{ TT }->{ config } ),
    
            layout => sub {
                my ( $self, $v, $body ) = @_;
                my $tt = $self->{ tt };
                $v->{ site_tit } = $App::CONFIG{ TT }->{ site_tit };
                $v->{ body     } = $body;
                
                $tt->process( 'layout'. $App::CONFIG{ TT }->{ postfix }, $v, \my $output)
                    or return view_error( $tt->error );
                return $output;
            },
    
            _ => sub {
                my ( $self, $v ) = @_;
                my $tt = $self->{ tt };
                $v->{ R } = \&R;
                
                $tt->process( 
                    tt_template( $self->{template} ) . $App::CONFIG{ TT }->{ postfix }, 
                    $v, \my $output) 
                    or return view_error( $tt->error );
                return $output;
            }
    
        ),
    );
    
    sub view_error {
        my ( $error ) = shift;
        warn $error;
        return "<pre>" . $error . "</pre>\n";   # really need to HTML encode $error here
    }
    
    sub tt_template { 
        my $template = shift;
        $template =~ s/^_//;
        return $template;
    }
    

    Thats better đŸ˜‰

    /I3az/

  2. November 4, 2008 12:16 pm

    Update to above comment.

    After chatting with Beppu this is the expected behaviour from Squatting and this keeps it in line with what Camping does when you request a view prefixed with a _

    So u can still use above “fix”. Alternatively just rename your template to be prefixed with a _

    /I3az/

Trackbacks

  1. CSS views in Squatting « transfixed but not dead!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: