Created
December 11, 2009 20:40
-
-
Save mschout/254491 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Template::Plugin::Singleton::PerTemplate; | |
use strict; | |
use base qw(Template::Plugin); | |
my $Instance; | |
sub new { | |
my ($class, $context) = @_; | |
my $tmpl = $context->stash->get('template'); | |
unless (defined $Instance) { | |
$Instance = $class->_new_instance($context); | |
} | |
if ($Instance->_need_reset($context)) { | |
$Instance = $class->_new_instance($context); | |
} | |
return $Instance; | |
} | |
sub _new_instance { | |
my ($class, $context) = @_; | |
my $tmpl = $context->stash->get('template'); | |
return bless { | |
_assets => {}, | |
_template => "$tmpl" | |
}, $class; | |
} | |
# determine if the plugin needs to be reset. Internally this checks if the | |
# current main template object has changed since the singleton was | |
# instantiated, and if so, return true. | |
sub _need_reset { | |
my ($self, $context) = @_; | |
my $current_tmpl = $context->stash->get('template'); | |
if ($self->{_template} eq "$current_tmpl") { | |
return 0; | |
} | |
return 1; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment