Adding return JSON on specific requeest feature to template.

This commit is contained in:
Martin Kamerbeek 2009-08-10 12:42:33 +00:00
parent 5675c1a3a3
commit 46829ac970
4 changed files with 38 additions and 2 deletions

View file

@ -39,6 +39,8 @@
- fixed #10733: viewing pending version tags
- added Survey JSON performance warning for people with non-wre-standard JSON modules/versions (Patrick Donelan, SDH Consulting)
- rfe #10423: Provide a way to access the instance of the thing that was added, modified, or deleted via workflow. (Eric Kennedy)
- Templates can now return a JSONified version of their params hash based on
requested Accept headers ( Martin Kamerbeek / Oqapi )
7.7.16
- fixed #10590: Session::DateTime->secondsToInterval doesn't allow 7 weeks

View file

@ -24,6 +24,7 @@ use WebGUI::Exception;
use Tie::IxHash;
use Clone qw/clone/;
use HTML::Packer;
use JSON qw{ to_json };
=head1 NAME
@ -590,6 +591,13 @@ A hash reference containing template variables and loops. Automatically includes
sub process {
my $self = shift;
my $vars = shift;
# Return a JSONinfied version of vars if JSON is the only requested content type.
if ( $self->session->request->headers_in->{Accept} eq 'application/json' ) {
$self->session->http->setMimeType( 'application/json' );
return to_json( $vars );
}
$self->prepare unless ($self->{_prepared});
my $parser = $self->getParser($self->session, $self->get("parser"));
my $template = $self->get('usePacked')

View file

@ -16,8 +16,9 @@ use WebGUI::Test;
use WebGUI::Session;
use WebGUI::Asset::Template;
use Exception::Class;
use Test::More tests => 37; # increment this value for each test you create
use Test::More tests => 39; # increment this value for each test you create
use Test::Deep;
use JSON qw{ from_json };
my $session = WebGUI::Test->session;
@ -47,6 +48,18 @@ ok($output =~ m/\bBBBBB\b/, "process() - variables");
ok($output =~ m/true/, "process() - conditionals");
ok($output =~ m/\b(?:XY){5}\b/, "process() - loops");
# See if template listens the Accept header
$session->request->headers_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};
my $newList = WebGUI::Asset::Template->getList($session, 'WebGUI Test Template');
ok(exists $newList->{$template->getId}, 'Uncommitted template exists returned from getList');

View file

@ -213,7 +213,7 @@ sub new {
my $this = shift;
my $class = ref($this) || $this;
my $headers = WebGUI::PseudoRequest::Headers->new();
my $self = {headers_out => $headers};
my $self = { headers_out => $headers, headers_in => {} };
bless $self, $class;
return $self;
}
@ -292,6 +292,19 @@ sub content_type {
#----------------------------------------------------------------------------
=head2 headers_in ( )
Mimics the behavior of Apache2::Request->headers_in.
=cut
sub headers_in {
my $self = shift;
return $self->{headers_in};
}
#----------------------------------------------------------------------------
=head2 headers_out ( )
Returns the PseudoRequst::Headers object stored in $self for access to the headers.