[Catalyst] XPath, DOM problem

Tomas Doran bobtfish at bobtfish.net
Tue Jul 8 10:37:27 BST 2008


On 8 Jul 2008, at 09:00, Herr Verdieck Götz wrote:

> Hi,
>
> I try to generate a site content menu from a xml file.
>
> In my TT file I have the following syntax:
> [%
>  # USE prod_tree = XML.Simple('config/xml/hele_de_tree.xml'); # OK  
> this works
>
>
>  USE xpath = XML.XPath('config/xml/hele_de_tree.xml');  # ERROR
>  #USE dom = XML.DOM;
>  #prod_tree = dom.parse('config/xml/sitemap.xml'); # ERROR
>
> -%]
>
> The XML.Simple version is ok, but
> when I try to use XPath - or DOM-  I get the message:
> Errot message:
> Couldn't render template "plugin error - Cannot open file 'config/ 
> xml/hele_de_tree.xml' at /Library/Perl/5.8.8/XML/XPath.pm line 53.
>
> I use Catalyst 5.7014 and Template-Toolkit-2.19.
>
>
> I also tested XPath with a small script without problems. So the  
> combination with TT or Catalyst causes the error.

I'd guess it's not finding the correct path..

You probably want to say something like:

USE xpath = XML.XPath(c.path_to('config', 'xml', 'hele_de_tree.xml'));

so that you are not relying on the PWD being what you expect.

However, there is a good argument that you shouldn't be doing this in  
a template - this is data/domain logic, and therefore separation of  
concerns says that this should be in the Model.

I'd create a model which creates the appropriate data structures for  
you, and a controller which defines the file path, feeds it to the  
model, and then pushes the returned data structure/objects into the  
stash, allowing the templates to them simply iterate over them.

This buys you a lot of future proofing, as if you start wanting to  
pre-process your XML in some way before passing it to the template,  
you can trivially do so, without having to change any of the code in  
your templates.

Cheers
Tom




More information about the Catalyst mailing list