On Sun, Jun 24, 2001 at 08:19:27PM +0100, Mark Fowler wrote:
> Quick background for this; Anyone who hasn't seen this poor example of
> code have a quick look at:
> 
>   http://london.pm.org/~mark/ttxpath/

If I didn't already mention, I read this a while back when you first
presented it to London.pm and thought it was a really good overview.  
It also inspired me to "fix" XPath properly (see below).

I was thinking this might be nice to add this as a 
Template::Tutorial::XPath document.  Any chance you could be persuaded?

> As you can see it does this by having a re-delegation block for each of
> the objects that the view has which then looks up the tag's name and
> calls that block. However, it's all rather icky and has all the code in
> Template Toolkit code. Is there any way I can get this logic out of the
> presentation layer?

The XPath plugin now adds a present() method to the 
XML::XPath::Node::Element and XML::XPath::Node::Text
packages to Do the Right Thing.  The Element package
also has a content() method to display element content
via a view.

This is also the same for the XML::DOM plugin, by the way.


#========================================================================
package XML::XPath::Node::Element;
#========================================================================

#------------------------------------------------------------------------
# present($view)
#
# Method to present an element node via a view.
#------------------------------------------------------------------------

sub present {
    my ($self, $view) = @_;
    $view->view($self->getName(), $self);
}

sub content {
    my ($self, $view) = @_;
    my $output = '';
    foreach my $node (@{ $self->getChildNodes }) {
    $output .= $node->present($view);
    }
    return $output;
}

#========================================================================
package XML::XPath::Node::Text;
#========================================================================

#------------------------------------------------------------------------
# present($view)
#
# Method to present a text node via a view.
#------------------------------------------------------------------------

sub present {
    my ($self, $view) = @_;
    $view->view('text', $self->string_value);
}


So you can now say:

  [% USE xpath = XML.XPath('myfile.xml');
     xnode = xpath.find('/foo');
     xview.print(node);
  %]

and within a view template:

  blah blah blah
  [% item.content(view) %]
  more blah blah blah


What do you think?  Are there any things that this doesn't make
possible/easy?

A


-- 
Andy Wardley <[EMAIL PROTECTED]>   Signature regenerating.  Please remain seated.
     <[EMAIL PROTECTED]>   For a good time: http://www.kfs.org/~abw/


Reply via email to