Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
diff --cc lib/WebGUI/Asset/Template.pm
index 5807910,9c7dfeb..0000000
--- a/lib/WebGUI/Asset/Template.pm
+++ b/lib/WebGUI/Asset/Template.pm
@@@ -255,17 -538,58 +255,53 @@@ A parser class to use. Defaults to "Web
sub getParser {
my $class = shift;
my $session = shift;
- my $parser = shift;
-
- # If parser is not in the config, throw an error message
- if ( $parser && $parser ne $session->config->get('defaultTemplateParser')
- && !any { $_ eq $parser } @{$session->config->get('templateParsers')} ) {
- WebGUI::Error::NotInConfig->throw(
- error => "Attempted to load template parser '$parser' that is not in config file",
- module => $parser,
- configKey => 'templateParsers',
- );
- }
- else {
- $parser ||= $session->config->get("defaultTemplateParser") || "WebGUI::Asset::Template::HTMLTemplate";
- }
+ my $parser = shift || $session->config->get("defaultTemplateParser") || "WebGUI::Asset::Template::HTMLTemplate";
- WebGUI::Pluggable::load( $parser );
- return $parser->new($session);
+ if ($parser eq "") {
+ return WebGUI::Asset::Template::HTMLTemplate->new($session);
+ } else {
+ eval("use $parser");
+ return $parser->new($session);
+ }
}
++<<<<<<< HEAD
+
++=======
+ #-------------------------------------------------------------------
+ #
+ # See the warning about using this on processVariableHeaders(). If no
+ # variables were captured, we'll return the empty string.
+
+ sub getVariableJson {
+ my ($class, $session) = @_;
+ my ($show, $vars, $json);
+
+ return ($show = $session->stow->get('showTemplateVars'))
+ && ($vars = $show->{vars})
+ && ($json = eval { JSON::encode_json($vars) })
+ && ($show->{startDelimiter} . $json . $show->{endDelimiter})
+ or '';
+ }
+
+ #-------------------------------------------------------------------
+
+ =head2 importAssetCollateralData ( data )
+
+ Override to import attachments from old versions of WebGUI
+
+ =cut
+
+ sub importAssetCollateralData {
+ my ( $self, $data, @args ) = @_;
+ if ( $data->{template_attachments} ) {
+ $self->update( { attachmentsJson => JSON::to_json($data->{template_attachments}) } );
+ }
+ return $self->SUPER::importAssetCollateralData( $data, @args );
+ }
+
+
++>>>>>>> 61245ba... Fix template preview to work with chunked content and template post-processing (in Layout, for instance)
#-------------------------------------------------------------------
=head2 indexContent ( )
@@@ -321,12 -692,106 +357,113 @@@ A hash reference containing template va
=cut
sub process {
++<<<<<<< HEAD
+ my $self = shift;
+ my $vars = shift;
+ $self->prepare unless ($self->{_prepared});
+ return $self->getParser($self->session, $self->get("parser"))->process($self->get("template"), $vars);
++=======
+ my $self = shift;
+ my $vars = shift;
+ my $session = $self->session;
+
+ if ($self->get('state') =~ /^trash/) {
+ my $i18n = WebGUI::International->new($session, 'Asset_Template');
+ $session->errorHandler->warn('process called on template in trash: '.$self->getId
+ .'. The template was called through this url: '.$session->asset->get('url'));
+ return $session->var->isAdminOn ? $i18n->get('template in trash') : '';
+ }
+ elsif ($self->get('state') =~ /^clipboard/) {
+ my $i18n = WebGUI::International->new($session, 'Asset_Template');
+ $session->errorHandler->warn('process called on template in clipboard: '.$self->getId
+ .'. The template was called through this url: '.$session->asset->get('url'));
+ return $session->var->isAdminOn ? $i18n->get('template in clipboard') : '';
+ }
+
+ # Return a JSONinfied version of vars if JSON is the only requested content type.
+ if ( defined $session->request && $session->request->headers_in->{Accept} eq 'application/json' ) {
+ $session->http->setMimeType( 'application/json' );
+ return to_json( $vars );
+ }
+
+ my $stow = $session->stow;
+ my $show = $stow->get('showTemplateVars');
+ if ( $show && $show->{assetId} eq $self->getId && $self->canEdit ) {
+ # This will never be true again, cause we're getting rid of assetId
+ delete $show->{assetId};
+ $show->{vars} = $vars;
+ $stow->set( showTemplateVars => $show );
+ }
+
+ $self->prepare unless ($self->{_prepared});
+ my $parser = $self->getParser($session, $self->get("parser"));
+ my $template = $self->get('usePacked')
+ ? $self->get('templatePacked')
+ : $self->get('template')
+ ;
+ my $output;
+ eval { $output = $parser->process($template, $vars); };
+ if (my $e = Exception::Class->caught) {
+ $session->log->error(sprintf "Error processing template: %s, %s, %s", $self->getUrl, $self->getId, $e->error);
+ my $i18n = WebGUI::International->new($session, 'Asset_Template');
+ $output = sprintf $i18n->get('template error').$e->error, $self->getUrl, $self->getId;
+ }
+ return $output;
++>>>>>>> 61245ba... Fix template preview to work with chunked content and template post-processing (in Layout, for instance)
}
+ #-------------------------------------------------------------------
+
+ # Used for debugging and the template test renderer.
+
+ # WARNING: Please do not rely on this behavior. It's a bit of a hack, and
+ # should not be considered part of the core API. Eventually, we will have
+ # introspectable template objects so that you can more easily (and
+ # efficiently) get this kind of information.
+
+ # If the first value for the 'X-Webgui-Template-Variables' header is our
+ # assetId, then in addition to processing the template, append add a json
+ # representation of our template variables to the response. The headers
+ # "X-Webgui-Template-Variables-Start" and "X-Webgui-Template-Variables-End"
+ # will contain the delimiters for the start and end of this content so that
+ # the user agent (who had to have stuck the header in in the first place) can
+ # parse it out. The delimiters will make the whole thing look like an xml
+ # comment (<!-- ... -->) just in case.
+
+ # We would just send the vars in the header, but different webservers have
+ # different limits on header field size and it's impossible to say whether our
+ # data will fit inside them or not.
+
+ # This is intended to be called earlier in the request cycle (in the Content
+ # URL handler) so that the headers get sent before any chunked content starts
+ # being set up. We set the stow here and check it during process() to see
+ # whether we need to include the delimited json. Later on, Content will call
+ # call getVariableJson to get the results.
+
+ {
+ my $head = 'X-Webgui-Template-Variables';
+ my @chr = ('0'..'9', 'a'..'z', 'A'..'Z');
+
+ sub processVariableHeaders {
+ my ($class, $session) = @_;
+ my $r = $session->request;
+ if (my $id = $r->headers_in->{$head}) {
+ my $rnd = join('', map { $chr[int(rand($#chr))] } (1..32));
+ my $out = $r->headers_out;
+ my $st = "<!-- $rnd ";
+ my $end = " $rnd -->";
+ $out->{"$head-Start"} = $st;
+ $out->{"$head-End"} = $end;
+ $session->stow->set(
+ showTemplateVars => {
+ assetId => $id,
+ startDelimiter => $st,
+ endDelimiter => $end,
+ }
+ );
+ }
+ }
+ }
#-------------------------------------------------------------------
diff --cc lib/WebGUI/URL/Content.pm
index 75fba54,8c24ba8..0000000
--- a/lib/WebGUI/URL/Content.pm
+++ b/lib/WebGUI/URL/Content.pm
@@@ -51,39 -64,55 +52,79 @@@ The Apache request handler for this pac
sub handler {
my ($request, $server, $config) = @_;
$request->push_handlers(PerlResponseHandler => sub {
- my $session = $request->pnotes('wgSession');
- WEBGUI_FATAL: {
- unless (defined $session) {
- $session = WebGUI::Session->open($server->dir_config('WebguiRoot'), $config->getFilename, $request, $server);
- return Apache2::Const::OK if ! defined $session;
+ my $session = WebGUI::Session->open($server->dir_config('WebguiRoot'), $config->getFilename, $request, $server);
+ WEBGUI_FATAL: foreach my $handler (@{$config->get("contentHandlers")}) {
+ my $output = eval { WebGUI::Pluggable::run($handler, "handler", [ $session ] )};
+ if ( my $e = WebGUI::Error->caught ) {
+ $session->errorHandler->error($e->package.":".$e->line." - ".$e->error);
+ $session->errorHandler->debug($e->package.":".$e->line." - ".$e->trace);
}
++<<<<<<< HEAD
+ elsif ( $@ ) {
+ $session->errorHandler->error( $@ );
+ }
+ else {
+ if ($output eq "chunked") {
+ if ($session->errorHandler->canShowDebug()) {
+ $session->output->print($session->errorHandler->showDebug(),1);
++=======
+ WebGUI::Asset::Template->processVariableHeaders($session);
+ foreach my $handler (@{$config->get("contentHandlers")}) {
+ my $output = eval { WebGUI::Pluggable::run($handler, "handler", [ $session ] )};
+ if ( my $e = WebGUI::Error->caught ) {
+ $session->errorHandler->error($e->package.":".$e->line." - ".$e->error);
+ $session->errorHandler->debug($e->package.":".$e->line." - ".$e->trace);
+ }
+ elsif ( $@ ) {
+ $session->errorHandler->error( $@ );
+ }
+ else {
+ if ($output eq "chunked") {
+ if ($session->errorHandler->canShowDebug()) {
+ $session->output->print($session->errorHandler->showDebug(),1);
+ }
+ last;
+ }
+ if ($output eq "empty") {
+ if ($session->errorHandler->canShowDebug()) {
+ $session->output->print($session->errorHandler->showDebug(),1);
+ }
+ last;
+ }
+ elsif (defined $output && $output ne "") {
+ $session->http->sendHeader;
+ $session->output->print($output);
+ if ($session->errorHandler->canShowDebug()) {
+ $session->output->print($session->errorHandler->showDebug(),1);
+ }
+ last;
++>>>>>>> 61245ba... Fix template preview to work with chunked content and template post-processing (in Layout, for instance)
}
- # Keep processing for success codes
- elsif ($session->http->getStatus < 200 || $session->http->getStatus > 299) {
- $session->http->sendHeader;
- last;
+ last;
+ }
+ elsif (defined $output && $output ne "") {
+ $session->http->sendHeader;
+ $session->output->print($output);
+ if ($session->errorHandler->canShowDebug()) {
+ $session->output->print($session->errorHandler->showDebug(),1);
}
+ last;
+ }
+ # Keep processing for success codes
+ elsif ($session->http->getStatus < 200 || $session->http->getStatus > 299) {
+ $session->http->sendHeader;
+ last;
}
}
}
++<<<<<<< HEAD
+ $session->close;
++=======
+ $session->output->print(
+ WebGUI::Asset::Template->getVariableJson($session), 1
+ );
+ $session->close if defined $session;
++>>>>>>> 61245ba... Fix template preview to work with chunked content and template post-processing (in Layout, for instance)
return Apache2::Const::OK;
});
$request->push_handlers(PerlMapToStorageHandler => sub { return Apache2::Const::OK });
diff --cc t/Asset/Template.t
index 756dd4b,ca8b13a..0000000
--- a/t/Asset/Template.t
+++ b/t/Asset/Template.t
@@@ -44,7 -48,52 +44,56 @@@ $var{variable} = "BBBBB"
$output = $template->process(\%var);
ok($output =~ m/\bBBBBB\b/, "process() - variables");
ok($output =~ m/true/, "process() - conditionals");
++<<<<<<< HEAD
+ok($output =~ m/\s(?:XY){5}\s/, "process() - loops");
++=======
+ ok($output =~ m/\b(?:XY){5}\b/, "process() - loops");
+
+ # See if template listens the Accept header
+ my $request = $session->request;
+ my $in = $request->headers_in;
+ my $out = $request->headers_out;
+ $in->{Accept} = 'application/json';
+
+ my $json = $template->process(\%var);
+ my $andNowItsAPerlHashRef = eval { from_json( $json ) };
+ ok( !$@, 'Accept = json, JSON is returned' );
+ cmp_deeply( \%var, $andNowItsAPerlHashRef, 'Accept = json, The correct JSON is returned' );
+
+ # Done, so remove the json Accept header.
+ delete $session->request->headers_in->{Accept};
+
+ # Testing the stuff-your-variables-into-the-body-with-delimiters header
+ my $oldUser = $session->user;
+
+ # log in as admin so we pass canEdit
+ $session->user({ userId => 3 });
+ my $hname = 'X-Webgui-Template-Variables';
+ $in->{$hname} = $template->getId;
+
+ # processRaw sets some session variables (including username), so we need to
+ # re-do it.
+ WebGUI::Asset::Template->processRaw($session,$tmplText,\%var);
+
+ # This has to get called to set up the stow good and proper
+ WebGUI::Asset::Template->processVariableHeaders($session);
+
+ $template->process(\%var);
+
+ my $output = WebGUI::Asset::Template->getVariableJson($session);
+
+ delete $in->{$hname};
+ my $start = delete $out->{"$hname-Start"};
+ my $end = delete $out->{"$hname-End"};
+ my ($json) = $output =~ /\Q$start\E(.*)\Q$end\E/;
+ $andNowItsAPerlHashRef = eval { from_json( $json ) };
+ cmp_deeply( $andNowItsAPerlHashRef, \%var, "$hname: json returned correctly" )
+ or diag "output: $output";
+
+ $session->user({ user => $oldUser });
+
+ # done testing the header stuff
++>>>>>>> 61245ba... Fix template preview to work with chunked content and template post-processing (in Layout, for instance)
my $newList = WebGUI::Asset::Template->getList($session, 'WebGUI Test Template');
ok(exists $newList->{$template->getId}, 'Uncommitted template exists returned from getList');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.