Skip to content

Instantly share code, notes, and snippets.

@mschout
Created December 11, 2009 20:40
Show Gist options
  • Save mschout/254491 to your computer and use it in GitHub Desktop.
Save mschout/254491 to your computer and use it in GitHub Desktop.
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