On Thu, Jan 12, 2006 at 02:13:07PM +0000, me said:
> Any suggestions?

Against all odds this 5 minute hack seems to work although I've got a 
horrible reason it might be working for the wrong reason. 

Currently it prints out

HELLO
HELLO

wotcha

HELLO BAR

Now all I need to do is change the translate() function to actually
translate using gettext and then somehow get the current language from a
Template::Plugin::Translate, presumably by dicking around with $context;

Simon


---- >8 ----


#!/usr/local/bin/perl -w

use strict;
use Template;
use Template::Parser;
use Data::Dumper;

my $template = join "", <DATA>;
my $parser   = Template::Parser->new({ FACTORY => "MyDirective" });
my $tt       = Template->new({ PARSER => $parser });

$tt->process(\$template);

package MyDirective;
use Template::Constants;
use Template::Exception;
use base qw(Template::Directive);
use vars qw( $VERSION $DEBUG $PRETTY $WHILE_MAX $OUTPUT );

$VERSION = sprintf("%d.%02d", q$Revision: 2.19 $ =~ /(\d+)\.(\d+)/);

$WHILE_MAX = 1000 unless defined $WHILE_MAX;
$PRETTY    = 0 unless defined $PRETTY;
$OUTPUT    = '$output .= ';


sub template {
    my ($class, $block) = @_;
    $block = pad($block, 2) if $PRETTY;

    return "sub { return '' }" unless $block =~ /\S/;

    return <<EOF;
sub {
    my \$context = shift || die "template sub called without 
context\\n";
    my \$stash   = \$context->stash;
    my \$output  = '';
    my \$error;

    eval { sub translate{uc(shift)};
           BLOCK: {
$block
    } };
    if (\$@) {
        \$error = \$context->catch(\$@, \\\$output);
        die \$error unless \$error->type eq 'return';
    }

    return \$output;
}
EOF
}


sub get {
    my ($class, $expr) = @_;
    $OUTPUT = '$output .=';
    return "$OUTPUT translate($expr);";
}

package main;
1;

__DATA__
[% SET foo = "hello" %]
[% foo %]
[% "hello" %]
[% IF foo.match("hello") %]
wotcha
[% END %]
[% "$foo BAR"%]


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

Reply via email to