[Catalyst] additional_template_paths in Catalyst::View::TT 0.21

Will Hawes info at whawes.co.uk
Thu Jan 12 20:19:30 CET 2006


Matt S Trout wrote:
> On Thu, Jan 12, 2006 at 10:23:11AM +0000, Will Hawes wrote:
>> A. Pagaltzis wrote:
>>> * Zbigniew Lukasiak <zzbbyy at gmail.com> [2006-01-11 15:00]:
>>>> $path_array = $self->include_path( );
>>>> @$path_array = ( qw/path another_path/ );
>>> @{ $self->include_path } = qw( path another_path );
>>>
>>> Regards,
>> Here's a patch to Catalyst::View::TT 0.21 to incorporate the contents of 
>> this thread in POD.
> 
> Nice stuff! Giant lines, though. Format it properly (i.e. 80 cols) and
> re-send it and I'll be happy to apply it.
> 

Oops. Attempt 2 attached, hopefully now with sensible line length.
-------------- next part --------------
--- TT.pm.orig	Wed Jan 11 10:31:40 2006
+++ TT.pm	Thu Jan 12 19:16:18 2006
@@ -169,12 +169,28 @@
 
 =head2 DYNAMIC INCLUDE_PATH
 
-It is sometimes needed to dynamically add additional paths to the
-INCLUDE_PATH variable of the template object. This can be done by setting
-'additional_include_paths' on stash to a referrence to an array with
-additional paths:
+Sometimes it is desirable to modify INCLUDE_PATH for your templates at run time.
 
-    $c->stash->{additional_template_paths} = [$c->config->{root} . '/test_include_path'];
+Additional paths can be added to the start of INCLUDE_PATH via the stash as
+follows:
+
+    $c->stash->{additional_template_paths} =
+        [$c->config->{root} . '/test_include_path'];
+
+If you need to add paths to the end of INCLUDE_PATH, there is also an
+include_path() accessor available:
+
+    push( @{ $c->view('TT')->include_path }, qw/path/ );
+
+Note that if you use include_path() to add extra paths to INCLUDE_PATH, you
+MUST check for duplicate paths. Without such checking, the above code will add
+"path" to INCLUDE_PATH at every request, causing a memory leak.
+
+A safer approach is to use include_path() to overwrite the array of paths
+rather than adding to it. This eliminates both the need to perform duplicate
+checking and the chance of a memory leak:
+
+    @{ $c->view('TT')->include_path } = qw/path another_path/;
 
 =head2 RENDERING VIEWS
 


More information about the Catalyst mailing list