On Fri, Sep 22, 2006 at 09:40:42AM -0500, Evan Carroll wrote:
> Could someone please juxtapose PROCESS w/ ARGS, vs, MACRO? I'm confused
> as to which one to use. PROCESS can operate on a named block, and send
> arguments into that block for invocation, MACRO does the same thing? Is
> the difference just semantic?


Might be helpful to look at a compiled template with a MACRO and
a BLOCK:

    $ cat test.pl
    use Template;
    use Template::Parser;
    use Template::Directive;

    $Template::Parser::DEBUG = 1;
    $Template::Directive::PRETTY = 1;

    Template->new->process(\*DATA);


    __END__

    This is plain text

    [% MACRO my_macro( var ) BLOCK;

        "Var is '$var'\n";
    END %]


    [%
        PROCESS some_block foo = 'bar';
        "more text here\n";
        my_macro( 'bar' );
    %]

    [% BLOCK some_block %]
        Foo is '[% foo %]'
    [% END %]

Which generates:

    $ perl test.pl
    [Template::Parser] compiled block 'some_block':
    sub {
        my $context = shift || die "template sub called without context\n";
        my $stash   = $context->stash;
        my $output  = '';
        my $error;
        
        eval { BLOCK: {
            $output .=  "\n    Foo is '";
    #line 17 "input file handle"
            $output .=  $stash->get('foo');
            $output .=  "'\n";
        } };
        if ($@) {
            $error = $context->catch($@, \$output);
            die $error unless $error->type eq 'return';
        }

        return $output;
    }
    [Template::Parser] compiled main template document block:
    sub {
        my $context = shift || die "template sub called without context\n";
        my $stash   = $context->stash;
        my $output  = '';
        my $error;
        
        eval { BLOCK: {
            $output .=  "\nThis is plain text\n\n";
    #line 0 "input file handle"
            
            # MACRO
            $stash->set('my_macro', sub {
                my $output = '';
                my (%args, $params);
                $args{ 'var' } = shift;
                $params = shift;
                $params = { } unless ref($params) eq 'HASH';
                $params = { %args, %$params };
            
                my $stash = $context->localise($params);
                eval {
    #line 0 "input file handle"
                    $output .=  ("Var is '" . $stash->get('var') . "'\n");
                };
                $stash = $context->delocalise();
                die $@ if $@;
                return $output;
            });
            
            $output .=  "\n\n\n";
    #line 10 "input file handle"
            $output .=  $context->process('some_block', { 'foo' => 'bar' });
    #line 10 "input file handle"
            $output .=  ('' . "more text here\n");
    #line 10 "input file handle"
            $output .=  $stash->get(['my_macro', [ 'bar' ]]);
            
            $output .=  "\n\n";
            
            $output .=  "\n\n\n\n\n\n";
        } };
        if ($@) {
            $error = $context->catch($@, \$output);
            die $error unless $error->type eq 'return';
        }

        return $output;
    }

    This is plain text





        Foo is 'bar'
    more text here
    Var is 'bar'









-- 
Bill Moseley
[EMAIL PROTECTED]


_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to