[Bps-public-commit] template-declare branch, master, updated. b6f5efe5d99c2e80daeaf9743eafe9d27c41e784

Thomas Sibley trs at bestpractical.com
Mon Dec 6 11:43:07 EST 2010


The branch, master has been updated
       via  b6f5efe5d99c2e80daeaf9743eafe9d27c41e784 (commit)
       via  6797fa13bd9852343962a9d197c26145986ca31a (commit)
       via  da0351b5acaef3263378b33ead0b0ce734c3b137 (commit)
      from  1ee89a18f7cc6ee3e11ae8f729704e4704176fc6 (commit)

Summary of changes:
 lib/Template/Declare.pm      |   16 ++++++++++++++++
 lib/Template/Declare/Tags.pm |   12 +++++++-----
 t/inline_xml_rendering.t     |   41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 5 deletions(-)
 create mode 100644 t/inline_xml_rendering.t

- Log -----------------------------------------------------------------
commit da0351b5acaef3263378b33ead0b0ce734c3b137
Author: Marc Chantreux <marc.chantreux at biblibre.com>
Date:   Wed Dec 1 22:09:20 2010 +0100

    Added support of $TAG_INDENTATION and $EOL
    
    spaces between tags are significant for web pages rendering and
    useless in most of cases. Template::Declare can now avoid them by
    setting two new variables:
    
    $Template::Declare::Tags::TAG_INDENTATION  = 0;
    $Template::Declare::Tags::EOL              = "";

diff --git a/lib/Template/Declare/Tags.pm b/lib/Template/Declare/Tags.pm
index 10d521c..e07ee54 100644
--- a/lib/Template/Declare/Tags.pm
+++ b/lib/Template/Declare/Tags.pm
@@ -33,6 +33,8 @@ our @TagSubs;
 our %ATTRIBUTES       = ();
 our %ELEMENT_ID_CACHE = ();
 our $TAG_NEST_DEPTH   = 0;
+our $TAG_INDENTATION  = 1;
+our $EOL              = "\n";
 our @TEMPLATE_STACK   = ();
 
 our $SKIP_XML_ESCAPING = 0;
@@ -600,7 +602,7 @@ of a template based on attributes passed to C<with>.
 sub smart_tag_wrapper (&) {
     my $coderef = shift;
 
-    Template::Declare->buffer->append("\n");
+    Template::Declare->buffer->append($EOL);
     Template::Declare->buffer->push( from => "T::D tag wrapper", private => 1 );
 
     my %attr = %ATTRIBUTES;
@@ -710,7 +712,7 @@ sub xml_decl (&;$) {
     while ( my ( $field, $val ) = splice( @rv, 0, 2 ) ) {
         outs_raw(qq/ $field="$val"/);
     }
-    outs_raw("?>\n");
+    outs_raw("?>$EOL");
     return @_;
 }
 
@@ -788,7 +790,7 @@ sub _tag {
     $tag = $tagset->namespace . ":$tag" if defined $tagset->namespace;
 
     Template::Declare->buffer->append(
-              "\n" 
+              $EOL
             . ( " " x $TAG_NEST_DEPTH )
             . "<$tag"
             . join( '',
@@ -824,7 +826,7 @@ sub _tag {
             wantarray ? () : '';
         };
 
-        local $TAG_NEST_DEPTH = $TAG_NEST_DEPTH + 1;
+        local $TAG_NEST_DEPTH = $TAG_NEST_DEPTH + $TAG_INDENTATION;
         %ATTRIBUTES = ();
         Template::Declare->buffer->push( private => 1, from => "T::D tag $tag" );
         $last = join '', map { ref($_) && $_->isa('Template::Declare::Tag') ? $_ : _postprocess($_) } $code->();
@@ -835,7 +837,7 @@ sub _tag {
 
     if (length $content) {
         Template::Declare->buffer->append(">$content");
-        Template::Declare->buffer->append("\n" . ( " " x $TAG_NEST_DEPTH )) if $content =~ /\</;
+        Template::Declare->buffer->append( $EOL . ( " " x $TAG_NEST_DEPTH )) if $content =~ /\</;
         Template::Declare->buffer->append("</$tag>");
     } elsif ( $tagset->can_combine_empty_tags($tag) ) {
         Template::Declare->buffer->append(" />");

commit 6797fa13bd9852343962a9d197c26145986ca31a
Author: Marc Chantreux <marc.chantreux at biblibre.com>
Date:   Thu Dec 2 00:11:25 2010 +0100

    test for inline rendering added
    
    $Template::Declare::Tags::TAG_INDENTATION  = 0;
    $Template::Declare::Tags::EOL              = "";
    
    must render without spaces between tags, i test that

diff --git a/t/inline_xml_rendering.t b/t/inline_xml_rendering.t
new file mode 100644
index 0000000..e751861
--- /dev/null
+++ b/t/inline_xml_rendering.t
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+package MyApp::Templates;
+use base 'Template::Declare';
+use Template::Declare::Tags;
+
+template main => sub {
+    html {
+	body { p { 'hi' } }
+    }
+}; 
+
+package main;
+use Test::More tests => 2;
+Template::Declare->init( dispatch_to => ['MyApp::Templates']);
+
+for
+( [ "
+<html>
+ <body>
+  <p>hi</p>
+ </body>
+</html>"
+]
+, [ "<html><body><p>hi</p></body></html>" => sub {
+	$Template::Declare::Tags::TAG_INDENTATION  = 0;
+	$Template::Declare::Tags::EOL              = "";
+    }
+] ) {
+
+    my ( $expected, $get_ready ) = @$_;
+    $get_ready and $get_ready->();
+    my $got      = Template::Declare->show('main');
+
+    for ($got,$expected) {
+	s/\n/Z/gxms;
+	s/\s/X/g;
+    } # easier to debug then :)
+
+    is $got, $expected;
+}

commit b6f5efe5d99c2e80daeaf9743eafe9d27c41e784
Author: Marc Chantreux <marc.chantreux at biblibre.com>
Date:   Thu Dec 2 00:31:31 2010 +0100

    doc added for tag indentation setting

diff --git a/lib/Template/Declare.pm b/lib/Template/Declare.pm
index a97763a..ded0be6 100644
--- a/lib/Template/Declare.pm
+++ b/lib/Template/Declare.pm
@@ -1019,6 +1019,22 @@ Wherein we will eventually provide a brief tutorial on creating custom tag sets.
 
 =end comment
 
+=head2 Indentation configuration
+
+by default, Template::Declare renders a readable xml adding end of lines and a
+one column indentation. This behavior could break a webpage design or add a
+significant amount of chars to your xml output. This could be changed by
+overwritting the default values. so
+
+    $Template::Declare::Tags::TAG_INDENTATION  = 0;
+    $Template::Declare::Tags::EOL              = "";
+    say Template::Declare->show('main');
+
+will render
+
+    <html><body><p>hi</p></body></html>
+
+
 =head1 METHODS
 
 =head2 init

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list