Andy,

> Fagyal Csongor wrote:
>> [% SET a = "
>> adsf
>> adsf
>> "
>> %]
>
> [...]
>
>> is compiled to
>>
>> "\n    adsf\n    \n    "
>
> No, it's not.  It's compiled to this:
>
> $stash->set('a', '
> adsf
> adsf
> ');
>
> TT works correctly in both this and your earlier PERL
> example.  Here's a test script:
>
>    use strict;
>    use warnings;
>    use Template;
>    use Template::Parser;
>    $Template::Parser::DEBUG = 1;
>
>    my $tt = Template->new(EVAL_PERL => 1);
>    $tt->process(\*DATA);
>
>    __END__
>    [% SET a = "
>    line one
>    line two
>    "
>    %]
>    <<[% a %]>>
>
>    [% PERL %]
>    my $xxx = <<__EOF__;
>    foo
>    bar
>    baz
>    __EOF__
>    print "<<$xxx>>";
>    [% END %]
>
> Here's the program output:
>
> <<
> line one
> line two
>  >>
>
> <<foo
> bar
> baz
>  >>
>
> Looks good to me.
>
> And here's the debug output showing the Perl code generated:
>
> [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: {
> #line 1 "input file handle"
> $stash->set('a', '
> line one
> line two
> ');
> $output .=  "\n<<";
> #line 6 "input file handle"
> $output .=  $stash->get('a');
> $output .=  ">>\n\n";
> #line 16 "input file handle"
>
> # PERL
> $context->throw('perl', 'EVAL_PERL not set')
>      unless $context->eval_perl();
>
> $output .=  do {
>      my $output = "package Template::Perl;\n";
>
> $output .=  "\nmy \$xxx = <<__EOF__;\nfoo\nbar\nbaz\n__EOF__\n\nprint
> \"<<\$xxx>>\";\n";
>
>      local($Template::Perl::context) = $context;
>      local($Template::Perl::stash)   = $stash;
>
>      my $result = '';
>      tie *Template::Perl::PERLOUT, 'Template::TieString', \$result; my
> $save_stdout = select *Template::Perl::PERLOUT;
>
>      eval $output;
>      select $save_stdout;
>      $context->throw($@) if $@;
>      $result;
> };
>
> $output .=  "\n";
>      } };
>      if ($@) {
>          $error = $context->catch($@, \$output);
>          die $error unless $error->type eq 'return';
>      }
>
>      return $output;
> }
>
> It's worth pointing out that you will get indenting added if you
> set the Template::Directives::PRETTY flag.  It's used to make the
> generated Perl code easier to read for humans debugging it, but
> shouldn't normally be enabled in a "live" system.  Perhaps that's
> the source of the confusion?  i.e. you set the flag to debug the
> code and you're seeing the prettified code with indenting.
>
> Without that flag set, TT should generate it as you wrote it.  No
> auto-indent.


Yes, it does work when the compilation is not flushed to/fetched from
disk. I forgot to mention that (my pages always worked OK at the first
run, and went wrong starting from the second run).

See this example:

use strict;
use warnings;
use Template;
use Slurp;

my $file = '/tmp/a.tem';
my $cfile = '/tmp/tmp/a.tem.comp';
unlink $cfile if -f $cfile;
print slurp $file;

my $tt = Template->new(
        EVAL_PERL => 1,
        'COMPILE_EXT' => '.comp',
        'COMPILE_DIR' => '/tmp',
        'ABSOLUTE' => 1,
        'CACHE_SIZE' => 0
);
print "-----------Processing template----------\n";
$tt->process($file) || die $tt->error;
$tt->process($file) || die $tt->error;
print slurp $cfile;

Produces:

[% SET a = "
line one
line two
"
%]
<<[% a %]>>
done.

-----------Processing template----------

<<
line one
line two
>>
done.

<<
    line one
    line two
    >>
done.
#------------------------------------------------------------------------
# Compiled template generated by the Template Toolkit version 2.15
#------------------------------------------------------------------------

Template::Document->new({
    METADATA => {
        'modtime' => '1163931584',
        'name' => '/tmp/a.tem',
    },
    BLOCK => sub {
        my $context = shift || die "template sub called without context\n";
        my $stash   = $context->stash;
        my $output  = '';
        my $error;

        eval { BLOCK: {
    #line 1 "/tmp/a.tem"
    $stash->set('a', '
    line one
    line two
    ');
    $output .=  "\n<<";
    #line 6 "/tmp/a.tem"
    $output .=  $stash->get('a');
    $output .=  ">>\ndone.\n";
        } };
        if ($@) {
            $error = $context->catch($@, \$output);
            die $error unless $error->type eq 'return';
        }

        return $output;
    },
    DEFBLOCKS => {

    },
});



- Fagzal



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

Reply via email to