From f3978ea1c30d283b9d46be0766a33c568ade511c Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 27 Feb 2013 14:18:26 +0100 Subject: [PATCH] SL::{Controller,Presenter}->render: $template kann auch ref auf Instanz von EscapedText sein MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Die folgenden Fälle werden nun für $template wie folgt behandelt: - keine Ref: $tempalte wird als Dateiname interpretiert - Ref auf einen String (ref eq 'SCALAR'): Zu rendernder String, direkte Übergabe an Template - Instanz von EscapedText und Ref auf Instanz von EscapedText: Zu rendernder String ist der von EscapedText geproxite Text, direkte Übergabe an Template mit "process => 1" und direktes Zurückgeben mit "process => 0" Damit ist es aus Caller-Sicht einfacher, den Aufruf richtig hinzubekommen: my $some_result = $self->presenter->grouped_record_list($objects); # Don't care whether or not $some_result is already an instance of # EscapedText: never treat this as a file name $self->render(\$some_result, { layout => 0 }); --- SL/Controller/Base.pm | 1 + SL/Presenter.pm | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/SL/Controller/Base.pm b/SL/Controller/Base.pm index aa65645ae..01e2db4f0 100644 --- a/SL/Controller/Base.pm +++ b/SL/Controller/Base.pm @@ -80,6 +80,7 @@ sub render { croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json)$/; # The "template" argument must be a string or a reference to one. + $template = ${ $template } if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText'); croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/); # If all output is turned off then don't output the header either. diff --git a/SL/Presenter.pm b/SL/Presenter.pm index 6658184c1..4547ec746 100644 --- a/SL/Presenter.pm +++ b/SL/Presenter.pm @@ -41,6 +41,7 @@ sub render { croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json)$/; # The "template" argument must be a string or a reference to one. + $template = ${ $template } if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText'); croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/); # Look for the file given by $template if $template is not a reference. @@ -61,7 +62,9 @@ sub render { # If no processing is requested then return the content. if (!$options->{process}) { # If $template is a reference then don't try to read a file. - return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if ref $template; + my $ref = ref $template; + return $template if $ref eq 'SL::Presenter::EscapedText'; + return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR'; # Otherwise return the file's content. my $file = IO::File->new($source, "r") || croak("Template file ${source} could not be read"); -- 2.20.1