On Thu, 13 May 2004, Kenny Gatdula wrote:

> Try setting the SearchPath for XML::Simple. [% USE xml = XML::Simple( 
> 'menu.xml' , {SearchPath => ['/var/www/site/data/'] }) %]

Cool. Amazing what you learn. But I don't like having to specify the 
paths in my files, they're different on dev and production. If only 
there was a way to infer this automagically...

<fx type="time passes" />

I am pleased to attach a patch that generates a search path list and
passes it to XMLin by introspecting the provider's include path. I freely
admit to cargo culting the introspection line from the archive [1] and I
think it will only work if there's just one provider but it works in my 
situation.

It also copes with XML::Simple's idea of what search paths should be (it 
doesn't allow relative paths and searchpaths to co-exist) and makes sure 
that XMLin sees only a filename and a list of directories.

My itch is scratched. I am happy :-)

Simon.
[1] 
http://template-toolkit.org/pipermail/templates/2001-November/002027.html

-- 
"Hi, I'm crazy Eddie! I put babies on spikes!"
 
--- Simple.orig 2004-05-14 12:58:52.000000000 +0100
+++ Simple.pm   2004-05-14 13:36:38.000000000 +0100
@@ -27,6 +27,8 @@
 use strict;
 use Template::Plugin;
 use XML::Simple;
+use File::Basename;
+use File::Spec;
 
 use base qw( Template::Plugin );
 use vars qw( $VERSION );
@@ -44,6 +46,20 @@
     my $input   = shift;
     my $args    = ref $_[-1] eq 'HASH' ? pop(@_) : { };
 
+    unless ( $input =~ /</ ) { #only for filenames
+        my $paths = $context->{ LOAD_TEMPLATES }->[0]->paths;
+    
+        my ($name, $path) = fileparse( $input );
+        if ($path) {
+            my @dirs = File::Spec->splitdir( $path );
+            my @fulldirs = map { File::Spec->catdir( $_, @dirs ) } @$paths;
+            $paths = [EMAIL PROTECTED];
+            $input = $name;
+        }
+        $args->{ searchpath } = $paths;
+
+    }
+    
     XMLin($input, %$args);
 }
 

Reply via email to