Permalink
Browse files

fixes POD

  • Loading branch information...
1 parent 0308a6d commit c18bb47c1b5ded75d2e3b75761177edc9fcd1608 @tomill committed Mar 3, 2010
Showing with 66 additions and 94 deletions.
  1. +33 −32 lib/Template/Semantic.pm
  2. +16 −40 lib/Template/Semantic/Cookbook.pod
  3. +17 −22 lib/Template/Semantic/Document.pm
View
@@ -78,18 +78,12 @@ Template::Semantic - Use pure XHTML/XML as a template
print Template::Semantic->process('template.html', {
'title, h1' => 'Naoki Tomita',
'ul.urls li' => [
- {
- 'a' => 'Profile & Contacts',
- 'a@href' => 'http://e8y.net/',
- },
- {
- 'a' => 'Twitter',
- 'a@href' => 'http://twitter.com/tomita/',
- },
+ { 'a' => 'Profile & Contacts', 'a@href' => 'http://e8y.net/', },
+ { 'a' => 'Twitter', 'a@href' => 'http://twitter.com/tomita/', },
],
});
-template:
+template.html
<html>
<head><title>person name</title></head>
@@ -120,8 +114,6 @@ Template::Semantic is a template engine for XHTML/XML based on L<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.
-B<This is beta release. Your feedback is welcome.>
-
=head1 METHODS
=over 4
@@ -152,19 +144,19 @@ Set if you want to replace XML parser. It should be L<XML::LibXML> based.
=item * (others)
-All other parameters are applied to the XML parser as method calls (C<<
-$parser->$key($value) >>). Template::Semantic uses this configuration by
-default:
+All other parameters are applied to the XML parser as method calls
+(C<< $parser->$key($value) >>). Template::Semantic uses this configuration
+by default:
no_newwork => 1 # faster
- recover => 2 # "no warnings" style parser
+ recover => 2 # "no warnings" style
See L<XML::LibXML::Parser/PARSER OPTIONS> for details.
- # "use strict;" style parser
+ # "use strict;" style
my $ts = Template::Semantic->new( recover => 0 );
- # "use warnings;" style parser
+ # "use warnings;" style
my $ts = Template::Semantic->new( recover => 1 );
=back
@@ -185,7 +177,7 @@ The first parameter is the input template, which may take one of several forms:
# text reference
my $res = Template::Semantic->process(\'<html><body>foo</body></html>', $vars);
- # file handle
+ # file handle, GLOB
my $res = Template::Semantic->process($fh, $vars);
my $res = Template::Semantic->process(\*DATA, $vars);
@@ -235,13 +227,19 @@ Use XPath expression or CSS selector as a selector.
});
-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).
+Template::Semantic allows some selector syntax that is different
+from usual XPath for your convenience.
+
+1. You can specify tag without using L<XML::LibXML::XPathContext>
+even if your template has default namespace (C<< <html xmlns="..." >>).
+
+2. You can use 'id()' function to find element with C<id=""> attribute
+instead of C<xml:id="">.
-Note 2: You can use 'id()' function in XHTML (with C<< <html xmlns="..." >>)
-without using L<XML::LibXML::XPathContext>. This module sets C<xmlns="">
-namespace declarations automatically if the template is like XHTML.
+3. If the expression looks like CSS selector, it is converted to XPath
+internally.
+
+4. You can specify the attribute by using '@attr' syntax with CSS selector.
=head1 VALUE TYPE
@@ -285,7 +283,7 @@ I<undef:> Delete the element/attirbute that the selector indicates.
=item * selector => XML::LibXML::Node
-Replace the inner content by the node. Note: XML::LibXML::Attr isn't supported.
+Replace the inner content by the node. XML::LibXML::Attr isn't supported.
$ts->process($template, {
'h1' => do { XML::LibXML::Text->new('foo') },
@@ -308,10 +306,10 @@ I<Hash-ref:> Sub query of the part.
'div.foo' => {
'a' => undef,
},
-
+
# same as above
'div.foo a' => undef,
-
+
# xpath '.' = current node (itself)
'a#bar' => {
'.' => 'foobar',
@@ -377,10 +375,13 @@ Output:
=item * selector => \&foo
-I<Code-ref:> Callback subroutine. The callback receives the inner HTML in
-C<$_> and an L<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.).
+I<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
@@ -390,7 +391,7 @@ to delete, etc.).
# sample: use $_
'h1' => sub { uc }, # <h1>foo</h1> => <h1>FOO</h1>
- # sample: use arg
+ # sample: use $_[0]
'h1' => sub {
my $node = shift;
$node->nodeName; # <h1>foo</h1> => <h1>h1</h1>
@@ -16,7 +16,7 @@ Use XPath power.
{ '//td[1]' => '401', '//td[2]' => '402' },
],
})->process({
- '//table//tr[position() mod 2 = 0]/@class' => 'even',
+ '//table/tbody/tr[position() mod 2 = 0]/@class' => 'even',
});
__DATA__
@@ -73,7 +73,7 @@ If you don't like C<class="">, do this.
'//table//tr[position() mod 2 = 1]/@class' => undef,
});
-=head2 For javascript
+=head2 For JavaScript vars
If you want to set the value for JavaScript...
@@ -89,7 +89,7 @@ If you want to set the value for JavaScript...
</html>
-=head2 Add attribute
+=head2 Create attribute
The following example is NOT output as C<< <div class="foo">foo</div> >>
but instead as C<< <div>foo</div> >> because LibXML cannot find C<class>
@@ -149,7 +149,7 @@ Output:
=head2 Indicator only for Temlate::Semantic
-Idea 1 - Original C<data-id> attribute:
+Idea 1 - Original C<data-id> attribute like L<Template::TAL>:
print Template::Semantic->process(\*DATA, {
'//*[@data-id="foo"]' => 'foo',
@@ -209,13 +209,14 @@ Output:
=head2 Email template?
-You should use the module like Template::Toolkit ;)
+You should use the module like Template-Toolkit ;)
But if you want to use this module anyway,
my $res = $ts->process(\*DATA, {
'name' => 'Foo & Co.',
});
+ my $subj = $res->dom->documentElement->findvalue('//*[@class="subject"]');
my $body = $res->dom->documentElement->findvalue('//*[@class="body"]');
__DATA__
@@ -237,50 +238,25 @@ But if you want to use this module anyway,
It is easy to substitute another C<process()>.
my $ts = Template::Semantic->new;
- print $ts->process(\*DATA, {
- '#container' => $ts->process('included.html'),
- });
-
- __DATA__
- <html>
- <body>
- <div>foo</div>
- <div id="container"></div>
- <div>bar</div>
- </body>
- </html>
-
-Do you want to include filename in the template?
-
- my $ts = Template::Semantic->new;
- print $ts->process(\*DATA, {
- '#container' => sub { $ts->process($_) },
+ print $ts->process('wrapper.html', {
+ '#container' => $ts->process('include.html'),
});
-
- __DATA__
- <html>
- <body>
- <div>foo</div>
- <div id="container">included.html</div>
- <div>bar</div>
- </body>
- </html>
-=head2 Wrapper
+=head2 Wrapper/Template inheritance
It is possible though is tricky.
my $ts = Template::Semantic->new;
- print $ts->process('content.html', {
- '/part' => sub {
+ print $ts->process('foo.html', {
+ '/div' => sub {
my $node = shift;
- # Get wrapper filename from attribute.
+ # Get wrapper filename from attribute ...
my $wrapper = $node->getAttribute('wrapper');
- # Process wrapper.html,
+ # Process wrapper.html ...
# embed this innerHTML to wrapper.html's <div id="content">
my $res = $ts->process($wrapper, { '#content' => \$_ });
@@ -294,12 +270,12 @@ It is possible though is tricky.
'h1' => 'Hello world!',
});
-content.html
+foo.html
- <part wrapper="wrapper.html">
+ <div wrapper="wrapper.html">
<h1>foo</h1>
<p>bla bla bla</p>
- </part>
+ </div>
wrapper.html
@@ -304,14 +304,11 @@ Template::Semantic::Document - Template::Semantic Result object
});
my $res = Template::Semantic->process('template.html', {
- '.foo, .bar' => 'baz',
- '.mee@class' => 'moo',
+ ...
})->process({
- '#boo@src' => 'grr',
- '#yea .ye' => 'yoo',
+ ...
})->process({
- '.foo' => sub { uc },
- '.bar' => sub { lc },
+ ...
});
print $res;
@@ -326,7 +323,7 @@ Template::Semantic::Document - Template::Semantic Result object
Process again to the result and returns L<Template::Semantic::Document>
object again. So you can chain
- Template::Semantic->process(...)->process(...)
+ my $res = Template::Semantic->process(...)->process(...)
=item "$res" (stringify)
@@ -338,10 +335,10 @@ Returns the result as XHTML/XML.
=over 4
-=item * is_xhtml => [1|0]
+=item * is_xhtml => bool
-Default value is true. Even if DTD is not defined in the template, outputs as XHTML.
-When sets C<is_xhtml> false, skip this effect.
+Default value is true. Even if DTD is not defined in the template,
+outputs as XHTML. When sets C<is_xhtml> false, skip this effect.
my $res = $ts->process(\<<END);
<div>
@@ -353,20 +350,18 @@ When sets C<is_xhtml> false, skip this effect.
;
print $res;
-
- <div>
- <img src="foo" />
- <br />
- <textarea></textarea>
- </div>
+ # <div>
+ # <img src="foo" />
+ # <br />
+ # <textarea></textarea>
+ # </div>
print $res->as_string(is_xhtml => 0);
-
- <div>
- <img src="foo"/>
- <br/>
- <textarea/>
- </div>
+ # <div>
+ # <img src="foo"/>
+ # <br/>
+ # <textarea/>
+ # </div>
=back

0 comments on commit c18bb47

Please sign in to comment.