Skip to content

Commit

Permalink
Add "strict" attribute
Browse files Browse the repository at this point in the history
Its make exceptional situations fatal. False by default, but consider changing in the future.
  • Loading branch information
theory committed Oct 31, 2009
1 parent e3dea85 commit b015b8a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,3 +1,6 @@
0.42
* Added the "strict" attribute to make exceptional situations fatal.

0.41 2009-10-29
* Documentation tweaks (Theory)

Expand Down
27 changes: 15 additions & 12 deletions lib/Template/Declare.pm
Expand Up @@ -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');
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/Template/Declare/Tags.pm
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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 '';
}
Expand Down
58 changes: 58 additions & 0 deletions 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';

0 comments on commit b015b8a

Please sign in to comment.