Template-Declare

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

            smart_tag_wrapper {
                my %params = @_; # set using 'with'
                form {
                    attr { %{ $params{attr} } };
                    $code->();
                    input { attr { type => 'submit', value => $params{value} } };
                };
            };
        }

        template edit_prefs => sub {
            with(
                attr  => { id => 'edit_prefs', action => 'edit.html' },
                value => 'Save'
            ), myform {
                label { 'Time Zone' };
                input { type is 'text'; name is 'tz' };
            };
        };

    Note in the "edit_prefs" template that we've used "with" to set up
    parameters to be passed to the smart wrapper. "smart_tag_wrapper()" is
    the device that allows you to receive those parameters, and also handles
    the magic of making sure that the tags you execute within it are
    properly output. Here we've used "myform" similarly to "form", only
    "myform" does something different with the "with()" arguments and
    outputs a submit element.

    Executing this template:

        Template::Declare->init( dispatch_to => ['My::Template'] );
        print Template::Declare->show('edit_prefs');

    Yields this output:

     <form action="edit.html" id="edit_prefs">
      <label>Time Zone</label>
      <input type="text" name="tz" />
      <input type="submit" value="Save" />
     </form>

  Class Search Dispatching
    The classes passed via the "dispatch_to" parameter to "init()" specify
    all of the templates that can be executed by subsequent calls to
    "show()". Template searches through these classes in order to find those
    templates. Thus it can be useful, when you're creating your template

lib/Template/Declare.pm  view on Meta::CPAN

        smart_tag_wrapper {
            my %params = @_; # set using 'with'
            form {
                attr { %{ $params{attr} } };
                $code->();
                input { attr { type => 'submit', value => $params{value} } };
            };
        };
    }

    template edit_prefs => sub {
        with(
            attr  => { id => 'edit_prefs', action => 'edit.html' },
            value => 'Save'
        ), myform {
            label { 'Time Zone' };
            input { type is 'text'; name is 'tz' };
        };
    };

Note in the C<edit_prefs> template that we've used
L<C<with>|Template::Declare::Tags/"with"> to set up parameters to be passed to
the smart wrapper. C<smart_tag_wrapper()> is the device that allows you to
receive those parameters, and also handles the magic of making sure that the
tags you execute within it are properly output. Here we've used C<myform>
similarly to C<form>, only C<myform> does something different with the
C<with()> arguments and outputs a submit element.

Executing this template:

    Template::Declare->init( dispatch_to => ['My::Template'] );
    print Template::Declare->show('edit_prefs');

Yields this output:

 <form action="edit.html" id="edit_prefs">
  <label>Time Zone</label>
  <input type="text" name="tz" />
  <input type="submit" value="Save" />
 </form>

=head2 Class Search Dispatching

The classes passed via the C<dispatch_to> parameter to C<init()> specify all
of the templates that can be executed by subsequent calls to C<show()>.
Template::Declare searches through these classes in order to find those

t/smart_tag_wrapper.t  view on Meta::CPAN

        smart_tag_wrapper {
            my %params = @_; # set using 'with'
            form {
                attr { map {$_ => $params{attr}{$_} } sort keys %{ $params{attr} } };
                $code->();
                input { attr { type => 'submit', value => $params{value} } };
            };
        };
    }

    template edit_prefs => sub {
        with(
            attr  => { id => 'edit_prefs', action => 'edit.html' },
            value => 'Save'
        ), myform {
            label { 'Time Zone' };
            input { type is 'text'; name is 'tz' };
        };
    };

    package main;
    Template::Declare->init( dispatch_to => ['My::Template'] );

ok my $output = Template::Declare->show('edit_prefs'), 'Get edit_prefs output';
is(
    $output,
    qq{

<form action="edit.html" id="edit_prefs">
 <label>Time Zone</label>
 <input type="text" name="tz" />
 <input type="submit" value="Save" />
</form>}, "got correct output for simple"
);



( run in 1.848 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )