NAME

Catalyst::View::TT::Alloy - Template::Alloy (TT) View Class

VERSION

version 0.00007

SYNOPSIS

# use the helper to create your View myapp_create.pl view TT::Alloy TT::Alloy

# configure in myapp.yml

'View::TT::Alloy':
  INCLUDE_PATH:
    - __path_to(root/src)__
    - __path_to(root/lib)__
  PRE_PROCESS: 'config/main'
  WRAPPER: 'site/wrapper'
  # optional
  TEMPLATE_EXTENSION: '.tt'
  CATALYST_VAR: 'Catalyst'

# example render view in lib/MyApp/Controller/Root.pm

sub default : Private {
    my ( $self, $c ) = @_;
    $c->stash->{template} = 'message.tt2';
    $c->stash->{message}  = 'Hello World!';
    return;
}

sub end : ActionClass('RenderView') {
}

# access variables from template

The message is: [% message %].

# example when CATALYST_VAR is set to 'Catalyst'
Context is [% Catalyst %]
The base is [% Catalyst.req.base %]
The name is [% Catalyst.config.name %]

# example when CATALYST_VAR isn't set
Context is [% c %]
The base is [% base %]
The name is [% name %]

DESCRIPTION

This is the Catalyst view for the TT emulator Template::Alloy.

Your application should define a view class which is a subclass of this module. The easiest way to achieve this is using script/myapp_create.pl (replacing myapp with the name of your application).

$ script/myapp_create.pl view TT::Alloy TT::Alloy

You can either manually forward to the TT::Alloy as normal, or use Catalyst::Action::RenderView to do it for you.

# In MyApp::Controller::Root

sub end : ActionClass('RenderView') { }

RATIONAL

Template::Alloy is a pure-perl module which emulates most common features of TT, and in some cases is faster too. See Template::Alloy::TT for details of which features are missing.

Catalyst::View::TT::Alloy is generally compatible with Catalyst::View::TT. The TIMER configuration option isn't supported, and the paths() alias to include_path() has been removed.

Although Template::Alloy emulates several other templating modules, the interface differs for each one. For this reason, this module only provides the TT interface.

DYNAMIC INCLUDE_PATH

Sometimes it is desirable to modify INCLUDE_PATH for your templates at run time.

Additional paths can be added to the start of INCLUDE_PATH via the stash as follows:

$c->stash->{additional_template_paths} =
    [$c->config->{root} . '/test_include_path'];

If you need to add paths to the end of INCLUDE_PATH, there is also an include_path() accessor available:

push( @{ $c->view('TT')->include_path }, qw/path/ );

Note that if you use include_path() to add extra paths to INCLUDE_PATH, you MUST check for duplicate paths. Without such checking, the above code will add "path" to INCLUDE_PATH at every request, causing a memory leak.

A safer approach is to use include_path() to overwrite the array of paths rather than adding to it. This eliminates both the need to perform duplicate checking and the chance of a memory leak:

$c->view('TT')->include_path([ qw/ path another_path / ]);

If you are calling render directly then you can specify dynamic paths by having a additional_template_paths key with a value of additonal directories to search. See "CAPTURING TEMPLATE OUTPUT" for an example showing this.

RENDERING VIEWS

The view plugin renders the template specified in the template item in the stash.

sub message : Global {
    my ( $self, $c ) = @_;

    $c->stash->{template} = 'message.tt2';

    $c->forward('MyApp::View::TT::Alloy');
}

If template isn't defined, then it builds the filename from Catalyst/action and the TEMPLATE_EXTENSION config setting. In the above example, this would be message.

The items defined in the stash are passed to Template::Alloy for use as template variables.

sub default : Private {
    my ( $self, $c ) = @_;
    $c->stash->{template} = 'message.tt2';
    $c->stash->{message}  = 'Hello World!';
    $c->forward('MyApp::View::TT::Alloy');
}

A number of other template variables are also added:

c      A reference to the context object, $c
base   The URL base, from $c->req->base()
name   The application name, from $c->config->{ name }

These can be accessed from the template in the usual way:

<message.tt2>:

The message is: [% message %]
The base is [% base %]
The name is [% name %]

The output generated by the template is stored in $c->response->body.

CAPTURING TEMPLATE OUTPUT

If you wish to use the output of a template for some other purpose than displaying in the response, e.g. for sending an email, this is possible using Catalyst::Plugin::Email and the render method:

sub send_email : Local {
  my ($self, $c) = @_;

  $c->email(
    header => [
      To      => 'me@localhost',
      Subject => 'A TT Email',
    ],
    body => $c->view('TT::Alloy')->render($c, 'email.tt', {
      additional_template_paths => [ $c->config->{root} . '/email_templates'],
      email_tmpl_param1 => 'foo'
      }
    ),
  );
# Redirect or display a message
}

METHODS

new

The constructor for the TT::Alloy view.

process

Renders the template specified in $c->stash->{template} or $c->action (the private name of the matched action. Calls render to perform actual rendering. Output is stored in $c->response->body.

render

Arguments: ($c, $template, \%args)

Renders the given template and returns output, or croaks on error.

The template variables are set to %$args if $args is a hashref, or $$c->stash otherwise. In either case the variables are augmented with base set to << $c-req->base >>, c to $c and name to $c->config->{name}. Alternately, the CATALYST_VAR configuration item can be defined to specify the name of a template variable through which the context reference ($c) can be accessed. In this case, the c, base and name variables are omitted.

config

This method allows your view subclass to pass additional settings to the TT configuration hash, or to set the options as below:

CATALYST_VAR

Allows you to change the name of the Catalyst context object. If set, it will also remove the base and name aliases, so you will have access them through <context>.

For example:

MyApp->config({
    name     => 'MyApp',
    root     => MyApp->path_to('root'),
    'View::TT::Alloy' => {
        CATALYST_VAR => 'Catalyst',
    },
});

message.tt2:

The base is [% Catalyst.req.base %]
The name is [% Catalyst.config.name %]
TEMPLATE_EXTENSION

A sufix to add when building the template name, when $c->stash->{template} is not set.

For example:

package MyApp::Controller::Test;
sub test : Local { .. }

Would by default look for a template in <root>/test/test.

If you set TEMPLATE_EXTENSION to '.tt', it will look for <root>/test/test.tt.

HELPERS

The Catalyst::Helper::View::TT::Alloy module is provided to create your view module. It is invoked by the myapp_create.pl script:

$ script/myapp_create.pl view TT::Alloy TT::Alloy

SUPPORT

Catalyst Mailing List:

http://lists.rawmode.org/mailman/listinfo/catalyst

GIT REPOSITORY

https://github.com/djzort/Catalyst-View-TT-Alloy

SEE ALSO

Catalyst, Catalyst::Helper::View::TT::Alloy, Template::Alloy

AUTHORS

Carl Franks, cfranks@cpan.org

Based on the code of Catalyst::View::TT, by

Sebastian Riedel, sri@cpan.org

Marcus Ramberg, mramberg@cpan.org

Jesse Sheidlower, jester@panix.com

Andy Wardley, abw@cpan.org

CONTRIBUTORS

Moritz Onken, onken@netcubed.de

Dean Hamstead dean@bytefoundry.com.au

COPYRIGHT

This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.