The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Catalyst::View::Semantic - Template::Semantic view for Catalyst

SYNOPSIS

package MyApp::View::Semantic;
use Moose;
extends 'Catalyst::View::Semantic';
__PACKAGE__->meta->make_immutable;
1;

setup end action in Root

sub auto : Private {
    my ($self, $c) = @_;
    $c->stash( js => [], css => [], jscode => '' );
}

sub end : ActionClass('RenderView') {
    my ($self, $c) = @_;
    return if $c->response->status > 200 || $c->response->body;
    $c->stash->{title} //= 'Default page title';
    unshift $c->stash->{css}, 'bootstrap.min'; # add default css
    unshift $c->stash->{js}, 'vendor/prototype'; # add default js
    $c->forward('View::Semantic', []);
}

create layout as root/template/layout.html

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
    <link href="" rel="stylesheet" />
    <script src=""> </script>
    <script type="application/x-javascript">
        //<![CDATA[
        Event.observe(document, 'dom:loaded', function() {
            /*code*/
        });
        //]]>
    </script>
</head>
<body> </body>
</html>

in MyApp::Controller::Foo

sub bar :Local {
    my ($self, $c) = @_;
    $c->stash( view => { a => [ map {
        '.' => $_->name,
        '@href' => $_->url
    } $c->model('MyDB::Item')->all ] });
}

create template for action as root/template/foo/bar.html

<div>list: <a href="#">item</a></div>

that's it.

DESCRIPTION

This is Catalyst view class for Template::Semantic with sane (hopefully) defaults and some useful methods like html layout and pager. Feel free to extend your view. Check the Template::Semantic::Cookbook and source of this module.

ATTRIBUTES

The following is a list of configuration attributes you can set in your global Catalyst configuration or locally as in:

   package MyApp::View::Semantic;
   use Moose;
   extends 'Catalyst::View::Semantic';

   __PACKAGE__->config(
       template_extension => 'xml', # override default extension
       wrapper => sub { html => { 'body > div#main' => shift } } # override default wrapper
   );

path

Used at the prefix path for where yout templates are stored. Defaults to $c->path_to('root','template').

template_extension

Optionally set the filename extension of your templates. Default: 'html'

process_key

Name of key in stash which contains processing instructions for default template(from action path) or template specified in $c->stash->{template}. Default: 'view'

template_key

Name of key in stash which contains name of template on which apply processing instructions. Default: 'template'

content_type

Set $c->response->content_type to this value unless specified. Default: 'text/html'

wrapper

Default wrapper callback - sets layout template name and Template::Semamtic instruction to insert wrapped content. Default sub { layout => { body => shift } }

css_key

A key in stash which contains title to include in default layout. Default: 'title'

css_key

A key in stash which contains array of css file names (without extension) to include in default layout. Default: 'css'

js_key

A key in stash which contains array of js file names (without extension) to include in default layout. Default: 'js'

jscode_key

A key in stash which contains string of js code to include in default layout. Default: 'jscode'

css_uri

Prefix location of css files. Default: '/static/css'

js_uri

Prefix location of js files. Default: '/static/js'

pager_template

Name of default pager template

METHODS

process($c, $code, $template)

Renders the template specified in <$template> or in $c->stash->{$self->template_key} or $c->namespace/$c->action (the private name of the matched action). $code or stash contents under $c->stash{$self->process_key} are passed to the underlying view object. If wrapper is defined, than result is wrapped using default layout.

Output is stored in $c->response->body and value of $c->response->content_type to text/html or whatever you configured as the "content_type" attribute unless this header has previously been set.

render($template, $code)

Returns Templace::Semantic::Document as result of processing $template with $code instructions.

layout($template, $code, $meta)

Renders html document. Check "SYNOPSIS" for default layout which should be in $template file. $meta should be a hash with corresponding keys ("title_key", "css_key", "js_key", "jscode_key")

pager($c, $pager, $template)

Renders pager snippet from $template or $self->pager_template using $pager ( Data::Page object ) You should have root/template/pager.html (or other path which corresponds to your configuration)

<div> <a class="prev" href="#">prev</a> <span> <a href="#" class="">middle</a> </span> <a class="next" href="#">next</a> </div>

engine

Access underlying Template::Semantic object

SUPPORT

AUTHOR

Yegor Korablev <egor@cpan.org>

LICENSE

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

SEE ALSO

Template::Semantic, HTML::Zoom, Text::Xslate, Template, Catalyst::View, Catalyst::View::HTML::Zoom