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

Template::Lace::ComponentCallback - Create a component easily from a coderef

SYNOPSIS

{
  package Local::Template::User;

  use Moo;
  with 'Template::Lace::ModelRole';

  has [qw/title story/] => (is=>'ro', required=>1);

  sub template {q[
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <div id='story'></div>
        <tag-anchor href='more.html' target='_top'>
          See More
        </tag-anchor>
      </body>
    </html>
  ]}

  sub process_dom {
    my ($self, $dom) = @_;
    $dom->title($self->title)
      ->at('#story')
      ->content($self->story);
  }
}

use Template::Lace::ComponentCallback;
my $factory = Template::Lace::Factory->new(
  model_class=>'Local::Template::User',
  component_handlers=>+{
    tag => {
      anchor => Template::Lace::ComponentCallback=>new(sub {
        my ($self, %attrs) = @_;
        return "<a href='$_{href}' target='$_{target}'>$_{content}</a>";
      }),
    },
  },
);

In this case %attrs are the results of processing attributes.

NOTE You might prefer to call this via Template::Lace::Utils instead.

DESCRIPTION

Lets you make quick and dirty components from a coderef. To make this even faster and dirtier we localize $_ to $self and %_ to %attrs.

METHODS

This class defines the following public instance methods:

sub make_dom

Create an instance of Template::Lace::DOM. Useful if you have complex component setup and transformation.

SEE ALSO

Template::Lace.

AUTHOR

Please See Template::Lace for authorship and contributor information.

COPYRIGHT & LICENSE

Please see Template::Lace for copyright and license information.