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

Mojolicious::Plugin::PetalTinyRenderer - Petal::Tiny renderer plugin

SYNOPSIS

  # Mojolicious
  $self->plugin('PetalTinyRenderer');

  # Mojolicious::Lite
  plugin 'PetalTinyRenderer';

DESCRIPTION

Mojolicious::Plugin::PetalTinyRenderer is a renderer for templates written for Petal::Tiny, which is a Perl implementation of the Template Attribute Language (TAL).

OPTIONS

Mojolicious::Plugin::PetalTinyRenderer supports the following option.

name

  # Mojolicious::Lite
  plugin PetalTinyRenderer => {name => 'petal'};

Handler name, defaults to tal.

encoding

Encoding of the template-files as supplied to open, defaults to :encoding(UTF-8).

STASH

The stash is directly available in the templates, see the use of foo in the example.

h

Helpers are available via the h entry in the stash.

 <a tal:attributes="href h/url_for --index" href="/">go back to index</a>

c

The current controller instance can be accessed as c.

 I see you are requesting a document from 
 <span tal:replace="c/req/headers/host">Lorem ipsum</span>.

USEFUL PATTERNS

Call helper-function without generating html (-- prefixes a literal string):

 <span tal:condition="true:h/layout --default" tal:omit-tag="" />

Use a temporary variable to hold dynamically generated string for helper function:

 <span tal:define="mytitle h/localization --login"
       tal:condition="true:h/title mytitle"
       tal:omit-tag="" />

Insert styled paragraph with error-message, if any (the structure keyword means don't escape returned html):

 <p style="color:red" tal:condition="true:message" tal:content="structure message">
   Error message
 </p>

Include other action/template:

 <span tal:replace="structure h/include --example/welcome" />

You can loop over Mojo::Collections:

 <li tal:repeat="key some_mojo_collection" tal:content="key" />

See Petal::Tiny for more.

Author's observation: If you need to write very complex TAL-constructs, maybe you should reconsider what belongs in the controller and what belongs in the template. TAL seems to be very good at exposing this anti-pattern.

EXAMPLE

 use Mojolicious::Lite;

 plugin 'PetalTinyRenderer';

 get '/' => sub {
     my $self = shift;
     $self->stash( foo => Mojo::Collection->new(1,2,3) );
     $self->render('index');
 };

 app->start;

 __DATA__

 @@ layouts/default.html.tal
 <!DOCTYPE html>
 <html>
   <head><title tal:content="title">Lorem</title></head>
   <body tal:content="structure h/content">Ipsum</body>
 </html>

 @@ index.html.tal
 <span tal:condition="true:h/layout --default" tal:omit-tag="" />
 <span tal:condition="true:h/title --Welcome" tal:omit-tag="" />

 <p tal:repeat="i foo"><span tal:replace="i"/>: Welcome to the PetalTinyRenderer plugin!</p>

SEE ALSO

Petal::Tiny, Mojolicious, Mojolicious::Guides, http://mojolicio.us.

AUTHOR

Lars Balker <lars@balker.dk>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by DK Hostmaster A/S.

This is free software, licensed under:

  The MIT (X11) License

The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.