[Jifty-commit] r2337 - in Template-Declare: lib/Template/Declare t
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Wed Dec 6 02:23:17 EST 2006
Author: jesse
Date: Wed Dec 6 02:23:17 2006
New Revision: 2337
Added:
Template-Declare/t/arg-declaration-styles.t
Modified:
Template-Declare/ (props changed)
Template-Declare/lib/Template/Declare/Tags.pm
Log:
r45839 at pinglin: jesse | 2006-12-06 01:06:54 -0500
r45842 at pinglin: jesse | 2006-12-06 02:13:13 -0500
* Added support for:
div {
attr( id => 'foo');
outs('my text');
}
In addition to the existing support for:
div {
{ id is 'foo' }
outs('my text');
}
Tested both.
Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm (original)
+++ Template-Declare/lib/Template/Declare/Tags.pm Wed Dec 6 02:23:17 2006
@@ -5,7 +5,7 @@
use vars qw/@EXPORT @EXPORT_OK $self/;
use base 'Exporter';
@EXPORT =
- qw( with template private show get_current_attr outs in_isolation $self
+ qw( with template private show get_current_attr attr outs in_isolation $self
Tr td ); # these two warns the user to use row/cell instead
our $DEPTH = 0;
@@ -20,6 +20,14 @@
die "Tr {...} and td {...} are invalid; use row {...} and cell {...} instead.";
}
+sub attr {
+ while (my $field = shift @_) {
+ my $val = shift @_;
+ # only defined whle in a tag context
+ append_attr( $field, $val );
+ }
+ '';
+}
sub outs {
$BUFFER .= join( '', grep { defined } @_ );
return '';
@@ -101,7 +109,7 @@
my $buf = "\n" . ( " " x $DEPTH ) . "<$tag"
. join( '',
map { qq{ $_="} . ( $ATTRIBUTES{$_} || '' ) . qq{"} }
- keys %ATTRIBUTES );
+ sort keys %ATTRIBUTES );
my $had_content = 0;
@@ -121,7 +129,8 @@
$field =~ s/__/:/g; # xml__lang is 'foo' ====> xml:lang="foo"
$field =~ s/_/-/g; # http_equiv is 'bar' ====> http-equiv="bar"
- my $val = "@_";
+ # Squash empty values, but not '0' values
+ my $val = join(' ', grep {defined $_ && $_ ne ''} @_);
use bytes;
$val =~ s/&/&/g;
@@ -132,10 +141,18 @@
$val =~ s/"/"/g;
$val =~ s/'/'/g;
+ append_attr($field,$val);
+ ''
+ };
+
+ local *append_attr = sub {
+ my $field = shift;
+ my $val = shift;
$buf .= qq[ $field="$val"];
'';
};
+
my $last = $code->();
Added: Template-Declare/t/arg-declaration-styles.t
==============================================================================
--- (empty file)
+++ Template-Declare/t/arg-declaration-styles.t Wed Dec 6 02:23:17 2006
@@ -0,0 +1,115 @@
+use warnings;
+use strict;
+
+
+package TestApp::UI;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+
+template content => sub {
+ with( id => 'body' ), div {
+ outs('This is my content');
+ };
+
+};
+
+template content_curly => sub {
+ div {
+ { id is 'body'}
+ outs('This is my content');
+ }
+};
+
+
+template content_explicit => sub {
+ div {
+ attr( id => 'body');
+ outs('This is my content');
+ }
+
+};
+
+template content_mixed => sub {
+ with ( class => 'text'), div {
+ { id is 'body'}
+ attr ( style => 'red' );
+ outs('This is my red body text');
+ }
+};
+
+template content_withs => sub {
+ with ( class => 'text', id=>'body', style => 'red'), div {
+ outs('This is my red body text');
+ }
+};
+
+template content_curlies => sub {
+ div {
+ { class is 'text', id is 'body', style is 'red'}
+ outs('This is my red body text');
+ }
+};
+
+template content_attrs => sub {
+ div {
+ attr ( class => 'text', id => 'body', style => 'red' );
+ outs('This is my red body text');
+ }
+};
+
+package Template::Declare::Tags;
+use Test::More tests => 33;
+use HTML::Lint;
+
+our $self;
+local $self = {};
+bless $self, 'TestApp::UI';
+
+for (qw(content content_curly content_explicit)){
+local $Template::Declare::Tags::BUFFER;
+ok_content(show($_),$_);
+}
+
+for (qw(content_mixed content_attrs content_withs content_curlies)){
+local $Template::Declare::Tags::BUFFER;
+ok_multicontent(show($_),$_);
+}
+
+
+sub ok_lint {
+ my $html = shift;
+
+ my $lint = HTML::Lint->new;
+
+ $lint->parse($html);
+ is( $lint->errors, 0, "Lint checked clean" );
+ foreach my $error ( $lint->errors ) {
+ #diag( $error->as_string );
+ }
+
+}
+
+sub ok_multicontent{
+my $simple = shift;
+my $test = shift;
+ok($simple =~ 'This is my red body text');
+ok ($simple =~ qr{^<div (.*?)>This is my red body text\s*</div>$}m, $test);
+ok ($simple =~ qr{class="text"}, $test);
+ok ($simple =~ qr{style="red"}, $test);
+ok ($simple =~ qr{id="body"}, $test);
+#diag ($simple);
+ok_lint($simple);
+}
+
+
+
+sub ok_content{
+my $simple = shift;
+my $test =shift;
+ok($simple =~ 'This is my content');
+ok ($simple =~ qr{<div id="body">This is my content\s*</div>},$test);
+#diag ($simple);
+ok_lint($simple);
+}
+
+1;
More information about the Jifty-commit
mailing list