[Jifty-commit] r6049 - jifty/trunk/lib/Jifty/View/Declare
Jifty commits
jifty-commit at lists.jifty.org
Mon Dec 1 01:09:55 EST 2008
Author: ruz
Date: Mon Dec 1 01:09:55 2008
New Revision: 6049
Modified:
jifty/trunk/lib/Jifty/View/Declare/Helpers.pm
Log:
* shuffle functions around, group them, update docs
Modified: jifty/trunk/lib/Jifty/View/Declare/Helpers.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View/Declare/Helpers.pm (original)
+++ jifty/trunk/lib/Jifty/View/Declare/Helpers.pm Mon Dec 1 01:09:55 2008
@@ -25,119 +25,190 @@
=head1 METHODS
+=head2 Arguments
+=head3 get args
+Returns arguments as set in the dispatcher or with L</set> below.
+If called in scalar context, pulls the first item in C<args> and returns it.
+If called in list context, returns the values of all items in C<args>.
-=head2 form CODE
-
-Takes a subroutine reference or block of perl as its only argument and renders it as a Jifty C<form>.
-
+ my $action = get('action');
+ my ($action, $object) = get(qw(action object));
=cut
- {
- no warnings qw/redefine/;
- sub form (&;$) {
- my $code = shift;
-
- smart_tag_wrapper {
- outs_raw( Jifty->web->form->start(@_) );
- $code->();
- outs_raw( Jifty->web->form->end );
- return '';
- };
+sub get {
+ if (wantarray) {
+ map { _get_single($_) } @_;
+ } else {
+ _get_single($_[0]);
}
- }
+}
+sub _get_single {
+ my $v = request->template_argument($_[0]) || request->argument( $_[0] );
+ return $v if defined $v;
-=head2 hyperlink
+ if (request->top_request ne request() and $v = request->top_request->template_argument($_[0])) {
+ if (ref $v) {
+ warn("The template argument '$_[0]' was not explicitly passed to the current region ('@{[request->path]}'), and thus will not work if the region is ever refreshed. Unfortunately, it is a reference, so it can't be passed explicitly either. You'll need to explicitly pass some stringification of what it is to the region.".Carp::longmess);
+ } else {
+ warn("The template argument '$_[0]' was not explicitly passed to the the current region ('@{[request->path]}'), and thus will not work if the region is ever refreshed. Try passing it explicitly?");
+ }
+ }
+ return undef;
+}
-Shortcut for L<Jifty::Web/link>.
+=head3 set key => val [ key => val ...]
+
+Sets arguments for later grabbing with L<get>.
=cut
-sub hyperlink(@) {
- _function_wrapper( link => @_);
+sub set {
+ while ( my ( $arg, $val ) = splice(@_, 0, 2) ) {
+ request->template_argument( $arg => $val );
+ }
}
-sub _function_wrapper {
- my $function = shift;
- Template::Declare->new_buffer_frame;
- my $once= Jifty->web->$function(@_)->render || '';
- my $content = Template::Declare->buffer->data() ||'';
- Template::Declare->end_buffer_frame;
- outs_raw( $content.$once);
- return '';
+=head2 HTML pages and layouts
+=head3 page
-}
+ template 'foo' => page {{ title is 'Foo' } ... };
+ or
-=head2 tangent
+ template 'foo' => page { title => 'Foo' } content { ... };
-Shortcut for L<Jifty::Web/tangent>.
+Renders an HTML page wrapped in L</wrapper>, after calling
+"/_elements/nav" and setting a content type. Generally, you shouldn't
+be using "/_elements/nav" but a Dispatcher rule instead.
-=cut
+If C<page/content> calling convention is used, the return value of the
+first sub will be passed into wrapper as the second argument as a
+hashref, as well as the last argument for the content sub.
+=cut
-sub tangent(@) {
- _function_wrapper( tangent => @_);
+sub page (&;$) {
+ unshift @_, undef if $#_ == 0;
+ my ( $meta, $code ) = @_;
+ sub {
+ my $self = shift;
+ Jifty->handler->apache->content_type('text/html; charset=utf-8');
+ my $wrapper = Jifty->app_class('View')->can('wrapper') || \&wrapper;
+ my @metadata = $meta ? $meta->() : ();
+ my $metadata = $#metadata == 0 ? $metadata[0] : {@metadata};
+ local *is::title = sub { Carp::carp "Can't use 'title is' when mixing mason and TD" };
+ $wrapper->( sub { $code->( $self, $metadata ) }, $metadata );
+ }
}
-=head2 redirect
+=head3 content
-Shortcut for L<Jifty::Web/redirect>.
+Helper function for page { ... } content { ... }, read L</page> instead.
=cut
-sub redirect(@) {
- Jifty->web->redirect(@_);
- return '';
+sub content (&;$) {
+ # XXX: Check for only 1 arg
+ return $_[0];
}
-=head2 new_action
+=head3 wrapper $coderef
-Shortcut for L<Jifty::Web/new_action>.
+Render a page. $coderef is a L<Template::Declare> coderef.
+This badly wants to be redone.
=cut
-sub new_action(@) {
- return Jifty->web->new_action(@_);
+sub wrapper {
+ my $app_class = get_current_attr('PageClass') || 'View::Page';
+ delete $Template::Declare::Tags::ATTRIBUTES{ 'PageClass' };
+
+ my $page_class = Jifty->app_class( $app_class );
+ $page_class = 'Jifty::View::Declare::Page'
+ unless Jifty::Util->_require( module => $page_class, quiet => 0 );
+ # XXX: fallback, this is ugly
+ Jifty::Util->require( $page_class );
+
+ my $content_code = shift;
+ my $meta = shift;
+ my $page = $page_class->new({ content_code => $content_code, _meta => $meta });
+
+ my ($spa) = Jifty->find_plugin('Jifty::Plugin::SinglePage');
+
+ # XXX: spa hooks should be moved to page handlers
+ if ($spa && $page->allow_single_page) {
+ # If it's a single page app, we want to either render a
+ # wrapper and then get the region or render just the content
+ if ( !Jifty->web->current_region ) {
+ $page->render_header;
+ $page->render_body(sub {
+ render_region( $spa->region_name,
+ path => Jifty->web->request->path );
+ });
+ $page->render_footer;
+ } else {
+ $page->done_header(1);
+ $page->render_page->();
+ }
+ }
+ else {
+ $page->render_body( sub { $page->render_page->() });
+ $page->render_footer;
+ }
}
+=head2 Forms and actions
-=head2 render_region
+=head3 form CODE
-A shortcut for Jifty::Web::PageRegion->new(@_)->render which does the
-Template::Declare magic necessary to not mix its output with your current
-page's.
+Takes a subroutine reference or block of perl as its only argument and renders it as a Jifty C<form>,
+for example:
+ new_action class => 'CreateTask';
+ form {
+ form_next_page url => '/';
+ render_action $action;
+ form_submit( label => _('Create') );
+ };
=cut
-sub render_region(@) {
- unshift @_, 'name' if @_ % 2;
- my $args = {@_};
- my $path = $args->{path} ||= '/__jifty/empty';
- if ($Template::Declare::Tags::self && $path !~ m|^/|) {
- $args->{path} = $Template::Declare::Tags::self->path_for($path);
+{
+ no warnings qw/redefine/;
+ sub form (&;$) {
+ my $code = shift;
+
+ smart_tag_wrapper {
+ outs_raw( Jifty->web->form->start(@_) );
+ $code->();
+ outs_raw( Jifty->web->form->end );
+ return '';
+ };
}
- local $Template::Declare::Tags::self = undef;
- Template::Declare->new_buffer_frame;
- Jifty::Web::PageRegion->new(%$args)->render;
- my $content = Template::Declare->buffer->data();
- Template::Declare->end_buffer_frame;
- Jifty->web->out($content);
}
+=head3 new_action
+
+Shortcut for L<Jifty::Web/new_action>.
+
+=cut
+
+sub new_action(@) {
+ return Jifty->web->new_action(@_);
+}
-=head2 render_action $action_object, $fields, $args_to_pass_to_action
+=head3 render_action $action_object, $fields, $args_to_pass_to_action
Renders an action out of whole cloth.
Arguments
-=over
+=over 4
=item $action_object
@@ -156,7 +227,6 @@
=back
-
=cut
sub render_action(@) {
@@ -168,7 +238,19 @@
}
}
-=head2 form_return
+=head2 render_param $action @args
+
+Takes an action and one or more arguments to pass to L<Jifty::Action->form_field>.
+
+=cut
+
+sub render_param {
+ my $action = shift;
+ outs_raw( $action->form_field(@_) );
+ return '';
+}
+
+=head3 form_return
Shortcut for L<Jifty::Web::Form/return>.
@@ -179,7 +261,7 @@
'';
}
-=head2 form_submit
+=head3 form_submit
Shortcut for L<Jifty::Web::Form/submit>.
@@ -190,184 +272,106 @@
'';
}
-=head2 form_next_page
+=head3 form_next_page
Shortcut for L<Jifty::Web::Form/next_page>.
=cut
-
sub form_next_page(@) {
Jifty->web->form->next_page(@_);
}
-=head2 request
-
-Shortcut for L<Jifty::Web/request>.
-
-=cut
-
-sub request {
- Jifty->web->request;
-}
-
-=head2 current_user
-
-Shortcut for L<Jifty::Web/current_user>.
-
-=cut
-
-
-sub current_user {
- Jifty->web->current_user;
-}
+=head2 Other functions and shortcuts
-=head2 get args
+=head3 hyperlink
-Returns arguments as set in the dispatcher or with L</set> below.
-If called in scalar context, pulls the first item in C<args> and returns it.
-If called in list context, returns the values of all items in C<args>.
+Shortcut for L<Jifty::Web/link>.
=cut
-sub get {
- if (wantarray) {
- map { _get_single($_) } @_;
- } else {
- _get_single($_[0]);
- }
+sub hyperlink(@) {
+ _function_wrapper( link => @_);
}
-sub _get_single {
- my $v = request->template_argument($_[0]) || request->argument( $_[0] );
- return $v if defined $v;
-
- if (request->top_request ne request() and $v = request->top_request->template_argument($_[0])) {
- if (ref $v) {
- warn("The template argument '$_[0]' was not explicitly passed to the current region ('@{[request->path]}'), and thus will not work if the region is ever refreshed. Unfortunately, it is a reference, so it can't be passed explicitly either. You'll need to explicitly pass some stringification of what it is to the region.".Carp::longmess);
- } else {
- warn("The template argument '$_[0]' was not explicitly passed to the the current region ('@{[request->path]}'), and thus will not work if the region is ever refreshed. Try passing it explicitly?");
- }
- }
- return undef;
+sub _function_wrapper {
+ my $function = shift;
+ Template::Declare->new_buffer_frame;
+ my $once = Jifty->web->$function(@_)->render || '';
+ my $content = Template::Declare->buffer->data() ||'';
+ Template::Declare->end_buffer_frame;
+ outs_raw( $content.$once);
+ return '';
}
-=head2 set key => val [ key => val ...]
+=head3 tangent
-Sets arguments for later grabbing with L<get>.
+Shortcut for L<Jifty::Web/tangent>.
=cut
-
-sub set {
- while ( my ( $arg, $val ) = splice(@_, 0, 2) ) {
- request->template_argument( $arg => $val );
- }
-
+sub tangent(@) {
+ _function_wrapper( tangent => @_);
}
-=head2 render_param $action @args
+=head3 redirect
-Takes an action and one or more arguments to pass to L<Jifty::Action->form_field>.
+Shortcut for L<Jifty::Web/redirect>.
=cut
-sub render_param {
- my $action = shift;
- outs_raw( $action->form_field(@_) );
+sub redirect(@) {
+ Jifty->web->redirect(@_);
return '';
}
-=head2 page
-
- template 'foo' => page {{ title is 'Foo' } ... };
-
- or
-
- template 'foo' => page { title => 'Foo' } content { ... };
+=head3 render_region
-Renders an HTML page wrapped in L</wrapper>, after calling
-"/_elements/nav" and setting a content type. Generally, you shouldn't
-be using "/_elements/nav" but a Dispatcher rule instead.
-
-If C<page/content> calling convention is used, the return value of the
-first sub will be passed into wrapper as the second argument as a
-hashref, as well as the last argument for the content sub.
+A shortcut for C<Jifty::Web::PageRegion->new(@_)->render> which does the
+L<Template::Declare> magic necessary to not mix its output with your current
+page's.
=cut
-sub page (&;$) {
- unshift @_, undef if $#_ == 0;
- my ( $meta, $code ) = @_;
- sub {
- my $self = shift;
- Jifty->handler->apache->content_type('text/html; charset=utf-8');
- my $wrapper = Jifty->app_class('View')->can('wrapper') || \&wrapper;
- my @metadata = $meta ? $meta->() : ();
- my $metadata = $#metadata == 0 ? $metadata[0] : {@metadata};
- local *is::title = sub { Carp::carp "Can't use 'title is' when mixing mason and TD" };
- $wrapper->( sub { $code->( $self, $metadata ) }, $metadata );
+sub render_region(@) {
+ unshift @_, 'name' if @_ % 2;
+ my $args = {@_};
+ my $path = $args->{path} ||= '/__jifty/empty';
+ if ($Template::Declare::Tags::self && $path !~ m|^/|) {
+ $args->{path} = $Template::Declare::Tags::self->path_for($path);
}
+ local $Template::Declare::Tags::self = undef;
+ Template::Declare->new_buffer_frame;
+ Jifty::Web::PageRegion->new(%$args)->render;
+ my $content = Template::Declare->buffer->data();
+ Template::Declare->end_buffer_frame;
+ Jifty->web->out($content);
}
-=head2 content
-Helper function for page { ... } content { ... }
+=head3 request
+
+Shortcut for L<Jifty::Web/request>.
=cut
-sub content (&;$) {
- # XXX: Check for only 1 arg
- return $_[0];
+sub request {
+ Jifty->web->request;
}
-=head2 wrapper $coderef
+=head3 current_user
-Render a page. $coderef is a L<Template::Declare> coderef.
-This badly wants to be redone.
+Shortcut for L<Jifty::Web/current_user>.
=cut
-sub wrapper {
- my $app_class = get_current_attr('PageClass') || 'View::Page';
- delete $Template::Declare::Tags::ATTRIBUTES{ 'PageClass' };
- my $page_class = Jifty->app_class( $app_class );
- $page_class = 'Jifty::View::Declare::Page'
- unless Jifty::Util->_require( module => $page_class, quiet => 1 );
- # XXX: fallback, this is ugly
- Jifty::Util->require( $page_class );
-
- my $content_code = shift;
- my $meta = shift;
- my $page = $page_class->new({ content_code => $content_code, _meta => $meta });
-
- my ($spa) = Jifty->find_plugin('Jifty::Plugin::SinglePage');
-
- # XXX: spa hooks should be moved to page handlers
- if ($spa && $page->allow_single_page) {
- # If it's a single page app, we want to either render a
- # wrapper and then get the region or render just the content
- if ( !Jifty->web->current_region ) {
- $page->render_header;
- $page->render_body(sub {
- render_region( $spa->region_name,
- path => Jifty->web->request->path );
- });
- $page->render_footer;
- } else {
- $page->done_header(1);
- $page->render_page->();
- }
- }
- else {
- $page->render_body( sub { $page->render_page->() });
- $page->render_footer;
- }
+sub current_user {
+ Jifty->web->current_user;
}
-=head2 js_handlers
+=head3 js_handlers
Allows you to put javascript handlers, a la
L<Jifty::Web::Form::Element>, onto arbitrary HTML elements:
More information about the Jifty-commit
mailing list