Hello, Cees.

Do you recall your message about a singleton Template object on the "TT in callback" thread on the Templates mailing list? Well, I liked it so much I went ahead and implemented it. I've attached a patch for CGI::Application::Plugin::TT version 0.04 if you're interested. Hopefully, you haven't already implemented it yourself. :-)

I've tested this a bit, and it works--at least, it reuses the Template object.

Please let me know what you think...

Thanks,
Chad.


--

C. Chad Wallace
The Lodging Company
http://skihills.com/
OpenPGP Public Key ID: 0x262208A0
--- lib/CGI/Application/Plugin/TT.pm	(revision 1001)
+++ lib/CGI/Application/Plugin/TT.pm	(working copy)
@@ -20,6 +20,8 @@
 
 $VERSION = '0.04';
 
+my $singleton_name = '__tt_singleton';
+
 ##############################################
 ###
 ###   tt_obj
@@ -34,7 +36,19 @@
   my $self = shift;
 
   if (!$self->{__TT_OBJ}) {
-    $self->{__TT_OBJ} = Template->new( $self->{__TT_CONFIG}->{TEMPLATE_OPTIONS} ) || carp "Can't load Template";
+    if ( $self->{__TT_CONFIG}->{USE_SINGLETON} )
+    {
+      my $singleton_ref = eval "\\\$" . $self->{__TT_CONFIG}->{SINGLETON_NAMESPACE} . "::$singleton_name";
+      if ( !$$singleton_ref )
+      {
+      	$$singleton_ref = Template->new( $self->{__TT_CONFIG}->{TEMPLATE_OPTIONS} ) || carp "Can't load Template";
+      }
+      $self->{__TT_OBJ} = $$singleton_ref
+    }
+    else
+    {
+      $self->{__TT_OBJ} = Template->new( $self->{__TT_CONFIG}->{TEMPLATE_OPTIONS} ) || carp "Can't load Template";
+    }
   }
   return $self->{__TT_OBJ};
 }
@@ -66,6 +80,16 @@
         $self->{__TT_CONFIG}->{TEMPLATE_OPTIONS} = delete $props->{TEMPLATE_OPTIONS};
       }
 
+      # Check for USE_SINGLETON
+      if ($props->{USE_SINGLETON}) {
+        $self->{__TT_CONFIG}->{USE_SINGLETON} = delete $props->{USE_SINGLETON};
+      }
+
+      # Check for SINGLETON_NAMESPACE
+      if ($props->{SINGLETON_NAMESPACE}) {
+        $self->{__TT_CONFIG}->{SINGLETON_NAMESPACE} = delete $props->{SINGLETON_NAMESPACE};
+      }
+
       # If there are still entries left in $props then they are invalid
       carp "Invalid option(s) (".join(', ', keys %$props).") passed to tt_config" if %$props;
     }

Reply via email to