[Jifty-commit] r6249 - in jifty/trunk: . lib/Jifty lib/Jifty/Subs lib/Jifty/View/Declare lib/Jifty/View/Mason share/web/templates

Jifty commits jifty-commit at lists.jifty.org
Thu Jan 15 23:44:55 EST 2009


Author: alexmv
Date: Thu Jan 15 23:44:55 2009
New Revision: 6249

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/Makefile.PL
   jifty/trunk/lib/Jifty/Handler.pm
   jifty/trunk/lib/Jifty/Subs/Render.pm
   jifty/trunk/lib/Jifty/View.pm
   jifty/trunk/lib/Jifty/View/Declare/BaseClass.pm
   jifty/trunk/lib/Jifty/View/Declare/Handler.pm
   jifty/trunk/lib/Jifty/View/Declare/Helpers.pm
   jifty/trunk/lib/Jifty/View/Mason/Handler.pm
   jifty/trunk/lib/Jifty/Web.pm
   jifty/trunk/lib/Jifty/Web/PageRegion.pm
   jifty/trunk/share/web/templates/autohandler

Log:
 r41028 at kohr-ah:  chmrr | 2009-01-15 23:44:13 -0500
 Better integration between views, using a common String::BufferStack


Modified: jifty/trunk/Makefile.PL
==============================================================================
--- jifty/trunk/Makefile.PL	(original)
+++ jifty/trunk/Makefile.PL	Thu Jan 15 23:44:55 2009
@@ -67,10 +67,11 @@
 requires('Params::Validate');
 requires('Scalar::Defer' => '0.12');
 requires('Shell::Command');
+requires('String::BufferStack' => '1.10');
 requires('String::Koremutake');
 requires('SUPER');
 requires('SQL::ReservedWords');
-requires('Template::Declare' => '0.26');                # Template::Declare::Tags
+requires('Template::Declare' => '0.35');                # Template::Declare::Tags
 requires('Test::Base');
 requires('Test::Log4perl');
 requires('Test::LongString');

Modified: jifty/trunk/lib/Jifty/Handler.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Handler.pm	(original)
+++ jifty/trunk/lib/Jifty/Handler.pm	Thu Jan 15 23:44:55 2009
@@ -27,6 +27,7 @@
 use base qw/Class::Accessor::Fast Jifty::Object/;
 use Jifty::View::Declare::Handler ();
 use Class::Trigger;
+use String::BufferStack;
 
 BEGIN {
     # Creating a new CGI object breaks FastCGI in all sorts of painful
@@ -48,7 +49,7 @@
 
 
 
-__PACKAGE__->mk_accessors(qw(dispatcher _view_handlers  cgi apache stash));
+__PACKAGE__->mk_accessors(qw(dispatcher _view_handlers cgi apache stash buffer));
 
 =head2 mason
 
@@ -82,6 +83,7 @@
     $self->dispatcher->import_plugins;
     eval { Jifty::Plugin::DumpDispatcher->dump_rules };
 
+    $self->buffer(String::BufferStack->new( out_method => \&Jifty::View::out_method ));
     $self->setup_view_handlers();
     return $self;
 }
@@ -123,8 +125,7 @@
 
 sub fallback_view_handler { 
    my $self = shift; 
-    return $self->view(Jifty->config->framework('View')->{'FallbackHandler'});
-    
+   return $self->view(Jifty->config->framework('View')->{'FallbackHandler'}); 
 }
 
 =head2 setup_view_handlers
@@ -141,7 +142,6 @@
     foreach my $class ($self->view_handlers()) {
         $self->_view_handlers->{$class} =  $class->new();
     }
-
 }
 
 =head2 view ClassName
@@ -155,7 +155,6 @@
     my $self = shift;
     my $class = shift;
     return $self->_view_handlers->{$class};
-
 }
 
 
@@ -243,7 +242,9 @@
 
         # Return from the continuation if need be
         unless (Jifty->web->request->return_from_continuation) {
+            $self->buffer->out_method(\&Jifty::View::out_method);
             $self->dispatcher->handle_request();
+            $self->buffer->flush_output;
         } 
 
         $self->call_trigger('before_cleanup', $args{cgi});
@@ -290,6 +291,8 @@
     $self->cgi(undef);
     $self->apache(undef);
     $self->stash(undef);
+    $self->buffer->pop for 1 .. $self->buffer->depth;
+    $self->buffer->clear;
 }
 
 1;

Modified: jifty/trunk/lib/Jifty/Subs/Render.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Subs/Render.pm	(original)
+++ jifty/trunk/lib/Jifty/Subs/Render.pm	Thu Jan 15 23:44:55 2009
@@ -102,16 +102,20 @@
     # Region's arguments come from explicit arguments only
     $region->arguments( $render_info->{arguments} );
 
-    my $region_content = '';
     $region->enter;
     # Also provide an 'event' argument, and fill in the render
     # arguments.  These don't show up in the region's arguments if
     # inspected, but do show up in the request.
+    Jifty->handler->buffer->push( private => 1 );
     $region->render_as_subrequest(
-        \$region_content,
         { %{$region->arguments}, event => $event_object, $event_object->render_arguments },
     );
-    $callback->( $render_info->{mode}, $region->qualified_name, $region_content, $render_info->{attrs} );
+    $callback->(
+        $render_info->{mode},       
+        $region->qualified_name,
+        Jifty->handler->buffer->pop,
+        $render_info->{attrs},
+    );
     $region->exit;
 }
 

Modified: jifty/trunk/lib/Jifty/View.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View.pm	(original)
+++ jifty/trunk/lib/Jifty/View.pm	Thu Jan 15 23:44:55 2009
@@ -37,22 +37,24 @@
     Jifty->web->session->set_cookie;
     my $r = Jifty->handler->apache;
 
+    # Send a header
     $r->content_type || $r->content_type('text/html; charset=utf-8'); # Set up a default
-
     unless ( $r->http_header_sent or not __PACKAGE__->auto_send_headers ) {
         Jifty->handler->send_http_header;
     }
 
-    # We could perhaps install a new, faster out_method here that
-    # wouldn't have to keep checking whether headers have been
-    # sent and what the $r->method is.  That would require
-    # additions to the Request interface, though.
     binmode *STDOUT;
+
+    # We now install a new, faster out_method that doesn't have to
+    # keep checking whether headers have been sent.
+    my $content;
     if ( my ($enc) = $r->content_type =~ /charset=([\w-]+)$/ ) {
-        print STDOUT map Encode::encode($enc, $_), grep {defined} @_;
+        $content = sub { print STDOUT map Encode::encode($enc, $_), @_; };
     } else {
-        print STDOUT grep {defined} @_;
+        $content = sub { print STDOUT @_; };
     }
+    Jifty->handler->buffer->out_method( $content );
+    $content->(@_);
 }
 
 =head1 SEE ALSO

Modified: jifty/trunk/lib/Jifty/View/Declare/BaseClass.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View/Declare/BaseClass.pm	(original)
+++ jifty/trunk/lib/Jifty/View/Declare/BaseClass.pm	Thu Jan 15 23:44:55 2009
@@ -40,22 +40,17 @@
     *{ $class . '::wrapper' } = sub {
         my $code = shift;
         my $args = shift || {};
-        # so in td handler, we made jifty::web->out appends to td
-        # buffer, we need it back for here before we call $code.
-        # someday we need to finish fixing the output system that is
-        # in Jifty::View.
-        my $td_out = \&Jifty::Web::out;
-        local *Jifty::Web::out = sub { shift->mason->out(@_) };
-
-        local *HTML::Mason::Request::content = sub {
-            local *Jifty::Web::out = $td_out;
-            $code->();
-            my $content = Template::Declare->buffer->data;
-            Template::Declare->buffer->clear;
-            $content;
-        };
 
-        Jifty->handler->fallback_view_handler->show('/_elements/wrapper', $args);
+        my $interp = Jifty->handler->view('Jifty::View::Mason::Handler')->interp;
+        my $req = $interp->make_request( comp => '/_elements/wrapper' );
+        my $wrapper = $interp->load("/_elements/wrapper");
+        local $HTML::Mason::Commands::m = $req;
+        local $HTML::Mason::Commands::r = Jifty->handler->apache;
+        $req->comp(
+            {content => sub {$code->()}, store => \$req->{request_buffer}},
+            $wrapper,
+            %{$args}
+        );
     }
 }
 

Modified: jifty/trunk/lib/Jifty/View/Declare/Handler.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View/Declare/Handler.pm	(original)
+++ jifty/trunk/lib/Jifty/View/Declare/Handler.pm	Thu Jan 15 23:44:55 2009
@@ -67,29 +67,9 @@
     my $self     = shift;
     my $template = shift;
 
-    no warnings qw/redefine/;
-    local *Jifty::Web::out = sub {
-        shift;    # Turn the method into a function
-        goto &Template::Declare::Tags::outs_raw;
-    };
-    
-    my $content = Template::Declare::Tags::show_page( $template, { %{Jifty->web->request->arguments}, %{Jifty->web->request->template_arguments || {}} } );
-    return unless defined $content;
-
-    my $r = Jifty->handler->apache;
-    $r->content_type || $r->content_type('text/html; charset=utf-8'); # Set up a default
-    unless ( $r->http_header_sent || Jifty->web->request->is_subrequest ) {
-        Jifty->web->session->set_cookie;
-        Jifty->handler->send_http_header;
-    }
-
-    binmode *STDOUT;
-    if ( my ($enc) = $r->content_type =~ /charset=([\w-]+)$/ ) {
-        print STDOUT Encode::encode($enc, $content);
-    } else {
-        print STDOUT $content;
-    }
-    return undef;
+    Template::Declare->buffer( Jifty->handler->buffer );
+    Template::Declare::Tags::show_page( $template, { %{Jifty->web->request->arguments}, %{Jifty->web->request->template_arguments || {}} } );
+    return; # Explicit return so TD call above is in void context, and appends instead of returning.
 }
 
 =head2 template_exists TEMPLATENAME
@@ -98,8 +78,9 @@
 
 =cut
 
-sub template_exists { my $pkg =shift;  
-
-return Template::Declare->resolve_template(@_);}
+sub template_exists {
+    my $pkg =shift;
+    return Template::Declare->resolve_template(@_);
+}
 
 1;

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	Thu Jan 15 23:44:55 2009
@@ -290,7 +290,7 @@
     Jifty->web->form->next_page(@_);
 }
 
-=head2 Other functions and shortcuts
+=head2 Other functions and shortcutxs
 
 =head3 hyperlink
 
@@ -304,11 +304,7 @@
 
 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); 
+    Template::Declare->buffer->append( Jifty->web->$function(@_)->render || '' );
     return '';
 }
 
@@ -409,14 +405,7 @@
 sub render_mason {
     my ($template, $args) = @_;
     my $mason = Jifty->handler->view('Jifty::View::Mason::Handler');
-    my $orig_out = $mason->interp->out_method || Jifty::View->can('out_method');
-
-    my $buf = '';
-    $mason->interp->out_method(\$buf);
     $mason->handle_comp($template, $args);
-    $mason->interp->out_method($orig_out);
-
-    Template::Declare->buffer->append($buf);
     return '';
 }
 

Modified: jifty/trunk/lib/Jifty/View/Mason/Handler.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View/Mason/Handler.pm	(original)
+++ jifty/trunk/lib/Jifty/View/Mason/Handler.pm	Thu Jan 15 23:44:55 2009
@@ -44,10 +44,10 @@
 =head2 new PARAMHASH
 
 Takes a number of key-value parameters; see L<HTML::Mason::Params>.
-Defaults the C<out_method> to L</out_method>, and the C<request_class>
-to L<HTML::Mason::Request::Jifty> (below).  Finally, adds C<h> and
-C<u> escapes, which map to L</escape_uri> and L<escape_utf8>
-respectively.
+Defaults the C<out_method> to appending to L<Jifty::Handler/buffer>
+and the C<request_class> to L<HTML::Mason::Request::Jifty> (below).
+Finally, adds C<h> and C<u> escapes, which map to L</escape_uri> and
+L<escape_utf8> respectively.
 
 =cut
 
@@ -56,7 +56,7 @@
 
     my %p = @_ || $package->config;
     my $self = $package->SUPER::new( request_class => 'HTML::Mason::Request::Jifty',
-                                     out_method => $package->can('out_method'),
+                                     out_method => sub {Jifty->handler->buffer->append(@_)},
                                      %p );
     $self->interp->compiler->add_allowed_globals('$r');
     $self->interp->set_escape( h => \&escape_utf8 );
@@ -98,6 +98,7 @@
                           [application =>  Jifty::Util->absolute_path( Jifty->config->framework('Web')->{'TemplateRoot'} )],
                          ],
         %{ Jifty->config->framework('Web')->{'MasonConfig'} },
+        autoflush => 1, # Now forced on
     );
 
     my $root_serial = 0;
@@ -163,8 +164,8 @@
 =cut
 
 sub template_exists {
-	my $self = shift;
-	return $self->interp->comp_exists(@_);
+    my $self = shift;
+    return $self->interp->comp_exists(@_);
 }
 
 

Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Thu Jan 15 23:44:55 2009
@@ -108,8 +108,8 @@
 =cut
 
 sub out {
-    my $m = shift->mason;
-    $m ? $m->out(@_) : Jifty::View::out_method(@_);
+    shift;
+    Jifty->handler->buffer->append(@_);
 }
 
 

Modified: jifty/trunk/lib/Jifty/Web/PageRegion.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/PageRegion.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/PageRegion.pm	Thu Jan 15 23:44:55 2009
@@ -272,18 +272,28 @@
 
 sub as_string {
     my $self = shift;
+    Jifty->handler->buffer->push(private => 1);
+    $self->render;
+    return Jifty->handler->buffer->pop;
+}
+
+=head2 render
+
+Calls L</enter>, outputs the results of L</as_string>, and then calls
+L</exit>.  Returns an empty string.
+
+=cut
+
+sub render {
+    my $self = shift;
+
+    $self->enter;
+    my $buffer = Jifty->handler->buffer;
 
-    if (Jifty->web->current_region ne $self) {
-        # XXX TODO: Possibly we should just call ->enter
-        warn "Attempt to call as_string on a region which is not the current region";
-        return "";
-    }
-    
     my %arguments = %{ $self->arguments };
 
     # undef arguments cause warnings. We hatesses the warnings, we do.
     defined $arguments{$_} or delete $arguments{$_} for keys %arguments;
-    my $result = "";
 
     # We need to tell the browser this is a region and what its
     # default arguments are as well as the path of the "fragment".  We
@@ -291,33 +301,37 @@
     # information.  We only render this region wrapper if we're asked
     # to (which is true by default)
     if ( $self->region_wrapper ) {
-        $result .= qq|<script type="text/javascript">\n|
+         $buffer->append(qq|<script type="text/javascript">\n|
             . qq|new Region('| . $self->qualified_name . qq|',|
             . Jifty::JSON::objToJson( \%arguments, { singlequote => 1 } ) . qq|,| 
             . qq|'| . $self->path . qq|',|
             . ( $self->parent ? qq|'| . $self->parent->qualified_name . qq|'| : q|null|)
             . qq|,| . (Jifty->web->form->is_open ? '1' : 'null')
             . qq|);\n|
-            . qq|</script>|;
+            . qq|</script>|);
         if ($self->lazy) {
-            $result .= qq|<script type="text/javascript">|
+            $buffer->append(qq|<script type="text/javascript">|
               . qq|jQuery(function(){ Jifty.update( { 'fragments': [{'region': '|.$self->qualified_name.qq|', 'mode': 'Replace'}], 'actions': {}}, document.getElementById('region-|.$self->qualified_name.qq|'))})|
-              . qq|</script>|;
+              . qq|</script>|);
         }
-        $result .= qq|<div id="region-| . $self->qualified_name . qq|" class="jifty-region">|;
+        $buffer->append(qq|<div id="region-| . $self->qualified_name . qq|" class="jifty-region">|);
         if ($self->lazy) {
             if ($self->loading_path) {
                 local $self->{path} = $self->loading_path;
-                $self->render_as_subrequest(\$result, \%arguments);
+                $self->render_as_subrequest(\%arguments);
             }
-            return $result . qq|</div>|;
+            $buffer->append(qq|</div>|);
         }
     }
 
-    $self->render_as_subrequest(\$result, \%arguments);
-    $result .= qq|</div>| if ( $self->region_wrapper );
+    # Make mason flush from its buffers into our String::BufferStack
+    $HTML::Mason::Commands::m->flush_buffer if $HTML::Mason::Commands::m;
+
+    $self->render_as_subrequest(\%arguments);
+    $buffer->append(qq|</div>|) if ( $self->region_wrapper );
 
-    return $result;
+    $self->exit;
+    return '';
 }
 
 =head2 render_as_subrequest
@@ -325,11 +339,7 @@
 =cut
 
 sub render_as_subrequest {
-    my ($self, $out_method, $arguments, $enable_actions) = @_;
-
-    my $orig_out = Jifty->handler->mason->interp->out_method || Jifty::View->can('out_method');
-
-    Jifty->handler->mason->interp->out_method($out_method);
+    my ($self, $arguments, $enable_actions) = @_;
 
     # Make a fake request and throw it at the dispatcher
     my $subrequest = Jifty->web->request->clone;
@@ -366,49 +376,12 @@
 	Jifty->web->request->top_request($top);
     }
 
-    # While we're inside this region, have Mason to tack its response
-    # onto a variable and not send headers when it does so
-    #XXX TODO: There's gotta be a better way to localize it
-
-    # template-declare based regions are printing to stdout
-    my $td_out = '';
-    {
-        open my $output_fh, '>>', \$td_out;
-        local *STDOUT = $output_fh;
-
-        local $main::DEBUG = 1;
-        # Call into the dispatcher
-        Jifty->handler->dispatcher->handle_request;
-    }
-
-    Jifty->handler->mason->interp->out_method($orig_out);
-
-    return unless length $td_out;
-
-    if ( my ($enc) = Jifty->handler->apache->content_type =~ /charset=([\w-]+)$/ ) {
-        $td_out = Encode::decode($enc, $td_out);
-    }
-    $$out_method .= $td_out;
+    # Call into the dispatcher
+    Jifty->handler->dispatcher->handle_request;
 
     return;
 }
 
-=head2 render
-
-Calls L</enter>, outputs the results of L</as_string>, and then calls
-L</exit>.  Returns an empty string.
-
-=cut
-
-sub render {
-    my $self = shift;
-
-    $self->enter;
-    Jifty->web->out($self->as_string);
-    $self->exit;
-    "";
-}
-
 =head2 get_element [RULES]
 
 Returns a CSS2 selector which selects only elements under this region

Modified: jifty/trunk/share/web/templates/autohandler
==============================================================================
--- jifty/trunk/share/web/templates/autohandler	(original)
+++ jifty/trunk/share/web/templates/autohandler	Thu Jan 15 23:44:55 2009
@@ -1,6 +1,4 @@
 <%init>
-$r->content_type('text/html; charset=utf-8');
-
 # XXX FIXME See Jifty::View::Mason::Handler for the source of this puke.
 use vars qw/ $jifty_internal_request /;
 if (!$jifty_internal_request && $m->base_comp->path =~ m|/_elements/|) {


More information about the Jifty-commit mailing list