diff --git a/Changes b/Changes index a499c38..38c356d 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,6 @@ +0.42 +* Added the "strict" attribute to make exceptional situations fatal. + 0.41 2009-10-29 * Documentation tweaks (Theory) diff --git a/lib/Template/Declare.pm b/lib/Template/Declare.pm index 94fd252..07a69c9 100644 --- a/lib/Template/Declare.pm +++ b/lib/Template/Declare.pm @@ -12,6 +12,7 @@ our $VERSION = '0.41'; use base 'Class::Data::Inheritable'; __PACKAGE__->mk_classdata('dispatch_to'); __PACKAGE__->mk_classdata('postprocessor'); +__PACKAGE__->mk_classdata('strict'); __PACKAGE__->mk_classdata('templates'); __PACKAGE__->mk_classdata('private_templates'); __PACKAGE__->mk_classdata('buffer'); @@ -473,6 +474,7 @@ Now let's have a look at how we use these templates with a post-processor: Template::Declare->init( dispatch_to => ['MyApp::Templates'], postprocessor => \&emphasize, + strict => 1, ); print Template::Declare->show( 'before' ); @@ -1056,6 +1058,12 @@ of the template itself. You can use this for instrumentation. For example: warn "Rendering $path took " . (time - $start) . " seconds."; }); +=item strict + +Die in exceptional situations, such as when a template can't be found, rather +than just warn. False by default for backward compatibility. The default may +be changed in the future, so specifying the value explicitly is recommended. + =back =cut @@ -1064,20 +1072,15 @@ sub init { my $class = shift; my %args = (@_); - if ( $args{'dispatch_to'} ) { - $class->dispatch_to( $args{'dispatch_to'} ); - } elsif ( $args{'roots'} ) { - $class->roots( $args{'roots'} ); - } - - if ( $args{'postprocessor'} ) { - $class->postprocessor( $args{'postprocessor'} ); - } - - if ( $args{'around_template'} ) { - $class->around_template( $args{'around_template'} ); + if ( $args{dispatch_to} ) { + $class->dispatch_to( $args{dispatch_to} ); + } elsif ( $args{roots} ) { + $class->roots( $args{roots} ); } + $class->strict( $args{strict} ) if exists $args{strict}; + $class->postprocessor( $args{postprocessor} ) if $args{postprocessor}; + $class->around_template( $args{around_template} ) if $args{around_template}; } =head2 show TEMPLATE_NAME diff --git a/lib/Template/Declare/Tags.pm b/lib/Template/Declare/Tags.pm index 9304049..dfbb228 100644 --- a/lib/Template/Declare/Tags.pm +++ b/lib/Template/Declare/Tags.pm @@ -465,8 +465,9 @@ sub with (@) { if ( lc($key) eq 'id' ) { if ( $ELEMENT_ID_CACHE{$val}++ ) { - warn - "HTML appears to contain illegal duplicate element id: $val"; + my $msg = "HTML appears to contain illegal duplicate element id: $val"; + die $msg if Template::Declare->strict; + warn $msg; } } @@ -866,6 +867,7 @@ sub _show_template { unless ($callable) { my $msg = "The template '$template' could not be found"; $msg .= " (it might be private)" if !$inside_template; + croak $msg if Template::Declare->strict; carp $msg; return ''; } diff --git a/t/strict.t b/t/strict.t new file mode 100644 index 0000000..7283db7 --- /dev/null +++ b/t/strict.t @@ -0,0 +1,58 @@ +use warnings; +use strict; + +package Wifty::UI; +use base qw/Template::Declare/; +use Template::Declare::Tags; + +template oops => sub { + with( id => 'foo', id => 'foo' ), html { + }; +}; + +package main; + +use Test::More tests => 11; +use Test::Warn; + +############################################################################## +Template::Declare->init(dispatch_to => ['Wifty::UI']); +pass 'Init with no strict setting'; + +warning_like { Template::Declare->show('nonesuch' ) } + qr/The template 'nonesuch' could not be found [(]it might be private[)]/, + 'Should get warning for nonexistent template'; + +warning_like { Template::Declare->show('oops' ) } + qr/HTML appears to contain illegal duplicate element id: foo/, + 'Should get warning for duplicate "id" attribute'; + +############################################################################## +Template::Declare->init(dispatch_to => ['Wifty::UI'], strict => 0); +pass 'Init with strict off'; + +warning_like { Template::Declare->show('nonesuch' ) } + qr/The template 'nonesuch' could not be found [(]it might be private[)]/, + 'Should still get warning for nonexistent template'; + +warning_like { Template::Declare->show('oops' ) } + qr/HTML appears to contain illegal duplicate element id: foo/, + 'Should still get warning for duplicate "id" attribute'; + +############################################################################## +Template::Declare->init(dispatch_to => ['Wifty::UI'], strict => 1); +pass 'Init with strict on'; + +undef $@; +eval { Template::Declare->show('nonesuch' ) }; +ok my $err = $@, 'Should get exception for missing template'; +like $err, + qr/The template 'nonesuch' could not be found [(]it might be private[)]/, + '... and it should be about nonexistent template'; + +undef $@; +eval { Template::Declare->show('oops' ) }; +ok my $err = $@, 'Should get exception for duplicate "id"'; +like $err, + qr/HTML appears to contain illegal duplicate element id: foo/, + '... and it should be about the duplicate "id" attribute';