A while back I posted to this list (June 6, 2001: COMPILE_DIR and TEMPLATE_DIR) a 
suggestion for cutting out some of the top level directories when saving compiled 
templates. This was so if all of your include paths shared the same higher directory 
structure:

/home/useracct/var/project1/templates
/home/useracct/var/project1b/data/templates

under the compile directory you set you'd only get

COMPILE_DIR/project1/templates
COMPILE_DIR/project1b/data/templates

instead of

COMPILE_DIR/home/useracct/var/project1/templates
COMPILE_DIR/home/useracct/var/project1b/data/templates


And I actually ran into a practical limit just today that caused me to revisit the 
problem where apparently my win2k box can't create a directory structure this deep:

f:/clients/towngreens/site/bin/data/cache/templates/f/clients/towngreens/site/bin/data/templates/html/exhibits/wrapper.tc


though I, of course, would be perfectly happy with:

f:/clients/towngreens/site/bin/data/cache/templates/html/exhibits/wrapper.tc


After my first post Andy brought up some of the real difficulties with doing such a 
thing, and of course my sense of scope at the time was much too small.  After taking 
the problems he posed into consideration, though, I've come up with the following 
code, to go somewhere in the Template::Provider's initialization sequence.  This is 
more proof of concept than actual code, and I'd like to hear people's feedback on it 
(including if Andy's even interested, of course) before I work up a patch:

#================================================
# During Initialization (After INCLUDE_PATH coercion to Array Ref )

if( defined $self->{SHORT_COMPILE_PATHS} and defined $self->{COMPILE_DIR} )
{
    # 'componentize' the paths in the include path
    my @paths =  map { [ split m|/|, $_ ] } @{$self->{INCLUDE_PATH}};

    # if there are multiple paths
    if( scalar(@paths) > 1 )
    {
        #See how far along all the paths share in common
        PTHS: while( scalar(@$paths[0]) )
        {
            my $directory_bit = pop @PATHS[0];
            
            foreach $next_directory ( @paths[1..$#PATHS] )
            {
                last PTHS if $directory_bit != pop @$next_directory;
            }
            
            $self->{COMMON_COMPILE_PREFIX} .= '/'.$directory_bit;
        }
    }
    # if there is only one path
    elsif( scalar(@paths) == 1 )
    {
        $self->{COMMON_COMPILE_PREFIX} .= join( '/', @$paths[0] );
    }
    # if there are no paths
    else
    {
        $self->{COMMON_COMPILE_PREFIX} .= '';
    }
}

#================================================
#Then when caching a file...(assuming $file_to_cache includes the absolute path)

my $prefix = $self->{COMMON_COMPILE_PREFIX};
$file_to_cache =~ s/$prefix//;


-Stephen Howard



Reply via email to