* 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:
## Callback
- selector => \\&foo
_Code-ref:_ Callback subroutine. The callback receives
$_ => innerHTML
$_[0] => XML::LibXML::Node object (X::L::Element, X::L::Attr, ...)
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 $_[0]
'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](https://metacpan.org/pod/Text%3A%3APipe) (`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](https://metacpan.org/pod/Template%3A%3ASemantic%3A%3AFilter).
- $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](https://metacpan.org/pod/Template%3A%3ASemantic%3A%3ACookbook)
[Template::Semantic::Document](https://metacpan.org/pod/Template%3A%3ASemantic%3A%3ADocument)
[XML::LibXML](https://metacpan.org/pod/XML%3A%3ALibXML), [HTML::Selector::XPath](https://metacpan.org/pod/HTML%3A%3ASelector%3A%3AXPath)
I got a lot of ideas from [Template](https://metacpan.org/pod/Template), [Template::Refine](https://metacpan.org/pod/Template%3A%3ARefine),
[Web::Scraper](https://metacpan.org/pod/Web%3A%3AScraper). 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.