[Rt-commit] rt branch, new-style-templates, updated. rt-3.8.8-186-gd54050d

Shawn Moore sartak at bestpractical.com
Tue Jul 20 23:49:28 EDT 2010


The branch, new-style-templates has been updated
       via  d54050dc424c799366bb4a179f83601ee35e1c90 (commit)
      from  bd8ce6688c6684589a98bf7bf74aa393bd758c47 (commit)

Summary of changes:
 lib/RT/Template_Overlay.pm |   57 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 56 insertions(+), 1 deletions(-)

- Log -----------------------------------------------------------------
commit d54050dc424c799366bb4a179f83601ee35e1c90
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Tue Jul 20 23:51:08 2010 -0400

    First cut of simple templates

diff --git a/lib/RT/Template_Overlay.pm b/lib/RT/Template_Overlay.pm
index f8fcbfb..f80ac8d 100755
--- a/lib/RT/Template_Overlay.pm
+++ b/lib/RT/Template_Overlay.pm
@@ -459,7 +459,62 @@ sub _ParseContentSimple {
         @_,
     );
 
-    return $args{Content};
+    my $template = Text::Template->new(
+        TYPE   => 'STRING',
+        SOURCE => $args{Content},
+    );
+    $template->compile;
+
+    # copied from Text::Template::fill_in and refactored to be simple variable
+    # interpolation
+    my $fi_r = '';
+    foreach my $fi_item (@{$template->{SOURCE}}) {
+        my ($fi_type, $fi_text, $fi_lineno) = @$fi_item;
+        if ($fi_type eq 'TEXT') {
+            $fi_r .= $fi_text;
+        } elsif ($fi_type eq 'PROG') {
+            my $fi_res;
+            my $interpolated;
+
+            my $original_fi_text = $fi_text;
+
+            # strip surrounding whitespace for simpler regexes
+            $fi_text =~ s/^\s+//;
+            $fi_text =~ s/\s+$//;
+
+            # if the codeblock is a simple $Variable lookup, use the value from
+            # the TemplateArgs hash...
+            if (my ($var) = $fi_text =~ /^\$(\w+)$/) {
+                if (exists $args{TemplateArgs}{$var}) {
+                    $fi_res = $args{TemplateArgs}{$var};
+                    $interpolated = 1;
+                }
+            }
+            # otherwise if it looks like a method call...
+            # XXX: this should be locked down otherwise you could say
+            # $TicketObj->Steal or something similarly ugly
+            elsif (my ($obj, $method) = $fi_text =~ /^\$(\w+)->(\w+)$/) {
+                if (exists $args{TemplateArgs}{$obj}) {
+                    $fi_res = $args{TemplateArgs}{$obj}->$method;
+                    $interpolated = 1;
+                }
+            }
+
+            # if there was no substitution then just reinsert the codeblock
+            if (!$interpolated) {
+                $fi_res = "{$original_fi_text}";
+            }
+
+            # If the value of the filled-in text really was undef,
+            # change it to an explicit empty string to avoid undefined
+            # value warnings later.
+            $fi_res = '' unless defined $fi_res;
+
+            $fi_r .= $fi_res;
+        }
+    }
+
+    return $fi_r;
 }
 
 sub _DowngradeFromHTML {

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


More information about the Rt-commit mailing list