[Rt-commit] rt branch, new-style-templates, updated. rt-3.8.8-218-g702a14d

Shawn Moore sartak at bestpractical.com
Mon Jul 26 19:52:15 EDT 2010


The branch, new-style-templates has been updated
       via  702a14d1989c29fb3684ac67b32be7ca6846e39e (commit)
      from  015855dce212a8e631c3afd7915756262d9f998f (commit)

Summary of changes:
 lib/RT/Template_Overlay.pm            |   41 +++++++++++++++++++++++++++++++++
 share/html/Admin/Global/Template.html |    3 ++
 share/html/Admin/Queues/Template.html |    3 ++
 3 files changed, 47 insertions(+), 0 deletions(-)

- Log -----------------------------------------------------------------
commit 702a14d1989c29fb3684ac67b32be7ca6846e39e
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Mon Jul 26 19:53:49 2010 -0400

    Compile-check template code as soon as it is saved

diff --git a/lib/RT/Template_Overlay.pm b/lib/RT/Template_Overlay.pm
index 4669179..12b8075 100755
--- a/lib/RT/Template_Overlay.pm
+++ b/lib/RT/Template_Overlay.pm
@@ -635,5 +635,46 @@ sub SetQueue {
     return $self->_Set( Field => 'Queue', Value => $NewQueueObj->id );
 }
 
+=head2 CompileCheck
+
+If the template's Type is Full, then compile check all the codeblocks to see if
+they are syntactically valid. We eval them in a codeblock to avoid actually
+executing the code.
+
+Returns an (ok, message) pair.
+
+=cut
+
+sub CompileCheck {
+    my $self = shift;
+
+    return (1, "Template does not include Perl code")
+        unless $self->Type eq 'Full';
+
+    my $template = Text::Template->new(
+        TYPE   => 'STRING',
+        SOURCE => $self->Content,
+    );
+    $template->compile;
+
+    # copied from Text::Template::fill_in and refactored to be compile checks
+    foreach my $fi_item (@{$template->{SOURCE}}) {
+        my ($fi_type, $fi_text, $fi_lineno) = @$fi_item;
+        next unless $fi_type eq 'PROG';
+
+        eval "sub { $fi_text }";
+        next if !$@;
+
+        my $error = $@;
+
+        # provide a (hopefully) useful line number for the error, but clean up
+        # all the other extraneous garbage
+        $error =~ s/\(eval \d+\) line (\d+).*/"template line " . ($1+$fi_lineno-1)/es;
+
+        return (0, $self->loc("Couldn't compile template codeblock '[_1]': [_2]", $fi_text, $error));
+    }
+
+    return (1, "Template compiles");
+}
 
 1;
diff --git a/share/html/Admin/Global/Template.html b/share/html/Admin/Global/Template.html
index fe83fce..d2a3f40 100755
--- a/share/html/Admin/Global/Template.html
+++ b/share/html/Admin/Global/Template.html
@@ -116,6 +116,9 @@ if ($TemplateObj->Id()) {
   		     	path => "Admin/Global/Template.html?Queue=0&Template=".$TemplateObj->Id(),
 			};
   push @results, @aresults;
+
+  my ($ok, $msg) = $TemplateObj->CompileCheck;
+  push @results, $msg if !$ok;
 }
 
 </%INIT>
diff --git a/share/html/Admin/Queues/Template.html b/share/html/Admin/Queues/Template.html
index 8366a75..27fe941 100755
--- a/share/html/Admin/Queues/Template.html
+++ b/share/html/Admin/Queues/Template.html
@@ -122,6 +122,9 @@ if ($TemplateObj->Id()) {
   		     	path => "Admin/Queues/Template.html?Queue=$Queue&Template=".$TemplateObj->Id(),
 			};
   push @results, @aresults;
+
+  my ($ok, $msg) = $TemplateObj->CompileCheck;
+  push @results, $msg if !$ok;
 } else {
   $QueueObj = RT::Queue->new($session{'CurrentUser'});
   $QueueObj->Load($Queue);

-----------------------------------------------------------------------


More information about the Rt-commit mailing list