[Rt-commit] r11424 - rtfm/branches/2.2-RELEASE/lib/RT/FM

ruz at bestpractical.com ruz at bestpractical.com
Thu Apr 3 02:48:03 EDT 2008


Author: ruz
Date: Thu Apr  3 02:48:03 2008
New Revision: 11424

Modified:
   rtfm/branches/2.2-RELEASE/lib/RT/FM/Article_Overlay.pm

Log:
* add ParseTemplate method

Modified: rtfm/branches/2.2-RELEASE/lib/RT/FM/Article_Overlay.pm
==============================================================================
--- rtfm/branches/2.2-RELEASE/lib/RT/FM/Article_Overlay.pm	(original)
+++ rtfm/branches/2.2-RELEASE/lib/RT/FM/Article_Overlay.pm	Thu Apr  3 02:48:03 2008
@@ -476,6 +476,64 @@
     }
 }
 
+=head2 ParseTemplate $CONTENT, %TEMPLATE_ARGS
+
+Parses $CONTENT string as a template (L<Text::Template>).
+$Article and other arguments from %TEMPLATE_ARGS are
+available in code of the template as perl variables.
+
+=cut
+
+sub ParseTemplate {
+    my $self = shift;
+    my $content = shift;
+    my %args = (
+        Ticket => undef,
+        @_
+    );
+
+    return ($content) unless defined $content && length $content;
+
+    $args{'Article'} = $self;
+    $args{'rtname'}  = $RT::rtname;
+    if ( $args{'Ticket'} ) {
+        my $t = $args{'Ticket'}; # avoid memory leak
+        $args{'loc'} = sub { $t->loc(@_) };
+    } else {
+        $args{'loc'} = sub { $self->loc(@_) };
+    }
+
+    foreach my $key ( keys %args ) {
+        next unless ref $args{ $key };
+        next if ref $args{ $key } =~ /^(ARRAY|HASH|SCALAR|CODE)$/;
+        my $val = $args{ $key };
+        $args{ $key } = \$val;
+    }
+
+    # We need to untaint the content of the template, since we'll be working
+    # with it
+    $content =~ s/^(.*)$/$1/;
+    my $template = Text::Template->new(
+        TYPE   => 'STRING',
+        SOURCE => $content
+    );
+
+    my $is_broken = 0;
+    my $retval = $template->fill_in(
+        HASH => \%args,
+        BROKEN => sub {
+            my (%args) = @_;
+            $RT::Logger->error("Article parsing error: $args{error}")
+                unless $args{error} =~ /^Died at /; # ignore intentional die()
+            $is_broken++;
+            return undef;
+        },
+    );
+    return ( undef, $self->loc('Article parsing error') ) if $is_broken;
+
+    return ($retval);
+}
+
 =head2 CurrentUserHasRight
 
 Returns true if the current user has the right for this article, for the whole system or for this article's class


More information about the Rt-commit mailing list