NAME Template::Semantic - Use pure XHTML/XML as a template SYNOPSIS use Template::Semantic; print Template::Semantic->process('template.html', { 'title, h1' => 'Naoki Tomita', 'ul.urls li' => [ { 'a' => 'Homepage >', 'a@href' => 'http://e8y.net/', }, { 'a' => 'Twitter >', 'a@href' => 'http://twitter.com/tomita/', }, ], }); template: person name

person name

output: Naoki Tomita

Naoki Tomita

DESCRIPTION Template::Semantic is a template engine for XHTML/XML based on XML::LibXML that doesn't use any template syntax. This module takes pure XHTML/XML as a template, and uses XPath or CSS selectors to assign values. This is beta release. Your feedback is welcome. METHODS $ts = Template::Semantic->new( %options ) Constructs a new "Template::Semantic" object. my $ts = Template::Semantic->new; my $out = $ts->process(...); If you do not want to change the options from the defaults, you may skip "new()" and call "process()" directly: my $out = Template::Semantic->process(...); Set %options if you want to change parser options: * parser => $your_libxml_parser Set if you want to replace XML parser. It should be XML::LibXML based. my $ts = Template::Semantic->new( parser => My::LibXML->new, ); * (others) All other parameters are applied to the XML parser as method calls ("$parser->$key($value)"). Template::Semantic uses this configuration by default: no_newwork => 1 # faster recover => 2 # "no warnings" style parser See "PARSER OPTIONS" in XML::LibXML::Parser for details. # "use strict;" style parser my $ts = Template::Semantic->new( recover => 0 ); # "use warnings;" style parser my $ts = Template::Semantic->new( recover => 1 ); $out = $ts->process($filename, \%vars) $out = $ts->process(\$text, \%vars) $out = $ts->process(FH, \%vars) Process a template and return a Template::Semantic::Document object. The first parameter is the input template, which may take one of several forms: # filename my $out = Template::Semantic->process('template.html', $vars); # text reference my $out = Template::Semantic->process(\'foo', $vars); # file handle my $out = Template::Semantic->process($fh, $vars); my $out = Template::Semantic->process(\*DATA, $vars); The second parameter is a value set to bind the template. $vars should be a hash-ref of selectors and corresponding values. See the "SELECTOR" and "VALUE TYPE" sections below. For example: { '.foo' => 'hello', '//title' => 'This is a title', } $ts->define_filter($filter_name, \&code) $ts->call_filter($filter_name) See the "Filter" section. SELECTOR Use XPath expression or CSS selector as a selector. print Template::Semantic->process($template, { # XPath sample that indicate '/html/body/h2[2]' => ..., '//title | //h1' => ..., '//img[@id="foo"]' => ..., 'id("foo")' => ..., # XPath sample that indicate @attr '//a[@id="foo"]/@href' => ..., '//meta[@name="keywords"]/@content' => ..., # CSS selector sample that indicate 'title' => ..., '.foo span.bar' => ..., '#foo' => ..., # CSS selector sample that indicate @attr 'img#foo@src' => ..., 'span.bar a@href' => ..., '@alt, @title' => ..., }); Note 1: CSS selectors are converted to XPath internally. You can use '@attr' in the selector to select a specific attribute to act on (unlike CSS, which only lets you select elements). Note 2: You can use 'id()' function in XHTML (with " $text *Scalar:* Replace the inner content with this as Text. $ts->process($template, { 'h1' => 'foo & bar', #

=> #

foo & bar

'.foo@href' => '/foo', # bar => # bar }); * selector => \$html *Scalar-ref:* Replace the inner content with this as fragment XML/HTML. $ts->process($template, { 'h1' => \'foobar', #

=> #

foobar

}); * selector => undef *undef:* Delete the element/attirbute that the selector indicates. $ts->process($template, { 'h1' => undef, #

foo

bar
=> #
bar
'div.foo@class' => undef, #
foo
=> #
foo
}); * selector => XML::LibXML::Node Replace the inner content by the node. Note: XML::LibXML::Attr isn't supported. $ts->process($template, { 'h1' => do { XML::LibXML::Text->new('foo') }, }); * selector => Template::Semantic::Document Replace the inner content by another "process()"-ed result. $ts->process('wrapper.html', { 'div#content' => $ts->process('inner.html', ...), }); * selector => { 'selector' => $value, ... } *Hash-ref:* Sub query of the part. $ts->process($template, { # All tag *in
* disappears 'div.foo' => { 'a' => undef, }, # same as above 'div.foo a' => undef, # xpath '.' = current node (itself) 'a#bar' => { '.' => 'foobar', './@href' => 'foo.html', }, # same as above 'a#bar' => 'foobar', 'a#bar/@href' => 'foo.html', }); Loop * selector => [ \%row, \%row, ... ] *Array-ref of Hash-refs:* Loop the part as template. Each item of the array-ref should be hash-ref. $ts->process(\*DATA, { 'table.list tr' => [ { 'th' => 'aaa', 'td' => '001' }, { 'th' => 'bbb', 'td' => '002' }, { 'th' => 'ccc', 'td' => '003' }, ], }); __DATA__
Output:
aaa 001
bbb 002
ccc 003
Callback * selector => \&foo *Code-ref:* Callback subroutine. The callback receives the inner HTML in $_ and an XML::LibXML::Node object as the first argument. Its return value is handled per this list of value types (scalar to replace content, undef to delete, etc.). $ts->process($template, { # samples 'h1' => sub { "bar" }, #

foo

=>

bar

'h1' => sub { undef }, #

foo

=> disappears # sample: use $_ 'h1' => sub { uc }, #

foo

=>

FOO

# sample: use arg 'h1' => sub { my $node = shift; $node->nodeName; #

foo

=>

h1

}, }); Filter * selector => [ $value, filter, filter, ... ] *Array-ref of Scalars:* Value and filters. Filters may be A) Callback subroutine (code reference) B) Defined filter name C) Object like Text::Pipe ("it->can('filter')") $ts->process($template, { 'h1' => [ 'foo', sub { uc }, sub { "$_!" } ], # =>

FOO!

'h2' => [ ' foo ', 'trim', sub { "$_!" } ], # =>

FOO!

'h3' => [ 'foo', PIPE('UppercaseFirst') ], # =>

Foo

}); Defined basic filters Some basic filters included. See Template::Semantic::Filter. $ts->define_filter($filter_name, \&code) You can define your own filters using "define_filter()". use Text::Markdown qw/markdown/; $ts->define_filter(markdown => sub { \ markdown($_) }) $ts->process($template, { 'div.content' => [ $text, 'markdown' ], }); $code = $ts->call_filter($filter_name) Accessor to defined filter. $ts->process($template, { 'div.entry' => ..., 'div.entry-more' => ..., })->process({ 'div.entry, div.entry-more' => $ts->call_filter('markdown'), }); SEE ALSO Template::Semantic::Cookbook Template::Semantic::Document XML::LibXML, HTML::Selector::XPath I got a lot of ideas from Template, Template::Refine, Web::Scraper. thanks! AUTHOR Naoki Tomita Feedback, patches, POD English check are always welcome! LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.