1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Module: HTree</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
<script type="text/javascript">
// <![CDATA[
function popupCode( url ) {
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
}
function toggleCode( id ) {
if ( document.getElementById )
elem = document.getElementById( id );
else if ( document.all )
elem = eval( "document.all." + id );
else
return false;
elemStyle = elem.style;
if ( elemStyle.display != "block" ) {
elemStyle.display = "block"
} else {
elemStyle.display = "none"
}
return true;
}
// Make codeblocks hidden by default
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
// ]]>
</script>
</head>
<body>
<div id="classHeader">
<table class="header-table">
<tr class="top-aligned-row">
<td><strong>Module</strong></td>
<td class="class-name-in-header">HTree</td>
</tr>
<tr class="top-aligned-row">
<td><strong>In:</strong></td>
<td>
<a href="../files/htree/modules_rb.html">
htree/modules.rb
</a>
<br />
<a href="../files/htree/context_rb.html">
htree/context.rb
</a>
<br />
<a href="../files/htree/display_rb.html">
htree/display.rb
</a>
<br />
<a href="../files/htree/doc_rb.html">
htree/doc.rb
</a>
<br />
<a href="../files/htree/elem_rb.html">
htree/elem.rb
</a>
<br />
<a href="../files/htree/encoder_rb.html">
htree/encoder.rb
</a>
<br />
<a href="../files/htree/equality_rb.html">
htree/equality.rb
</a>
<br />
<a href="../files/htree/extract_text_rb.html">
htree/extract_text.rb
</a>
<br />
<a href="../files/htree/fstr_rb.html">
htree/fstr.rb
</a>
<br />
<a href="../files/htree/htmlinfo_rb.html">
htree/htmlinfo.rb
</a>
<br />
<a href="../files/htree/inspect_rb.html">
htree/inspect.rb
</a>
<br />
<a href="../files/htree/leaf_rb.html">
htree/leaf.rb
</a>
<br />
<a href="../files/htree/loc_rb.html">
htree/loc.rb
</a>
<br />
<a href="../files/htree/name_rb.html">
htree/name.rb
</a>
<br />
<a href="../files/htree/output_rb.html">
htree/output.rb
</a>
<br />
<a href="../files/htree/parse_rb.html">
htree/parse.rb
</a>
<br />
<a href="../files/htree/raw_string_rb.html">
htree/raw_string.rb
</a>
<br />
<a href="../files/htree/rexml_rb.html">
htree/rexml.rb
</a>
<br />
<a href="../files/htree/scan_rb.html">
htree/scan.rb
</a>
<br />
<a href="../files/htree/tag_rb.html">
htree/tag.rb
</a>
<br />
<a href="../files/htree/template_rb.html">
htree/template.rb
</a>
<br />
<a href="../files/htree/text_rb.html">
htree/text.rb
</a>
<br />
<a href="../files/htree/traverse_rb.html">
htree/traverse.rb
</a>
<br />
</td>
</tr>
</table>
</div>
<!-- banner header -->
<div id="bodyContent">
<div id="contextContent">
<div id="description">
<h1>Template Engine</h1>
<p>
The htree template engine converts HTML and some data to HTML or XML.
</p>
<h2>Template Method Summary</h2>
<ul>
<li><a
href="HTree.html#M000008">HTree.expand_template</a>(<em>template_pathname</em>)
-> $stdout
</li>
<li><a
href="HTree.html#M000008">HTree.expand_template</a>(<em>template_pathname</em>,
<em>obj</em>) -> $stdout
</li>
<li><a
href="HTree.html#M000008">HTree.expand_template</a>(<em>template_pathname</em>,
<em>obj</em>, <em>out</em>) -> <em>out</em>
</li>
<li><a
href="HTree.html#M000008">HTree.expand_template</a>(<em>template_pathname</em>,
<em>obj</em>, <em>out</em>, <em>encoding</em>) -> <em>out</em>
</li>
<li><a
href="HTree.html#M000008">HTree.expand_template</a>{<em>template_string</em>}
-> $stdout
</li>
<li><a href="HTree.html#M000008">HTree.expand_template</a>(<em>out</em>)
{<em>template_string</em>} -> <em>out</em>
</li>
<li><a href="HTree.html#M000008">HTree.expand_template</a>(<em>out</em>,
<em>encoding</em>) {<em>template_string</em>} -> <em>out</em>
</li>
<li><a
href="HTree.html#M000009">HTree.compile_template</a>(<em>template_string</em>)
-> Module
</li>
<li><a href="HTree.html">HTree</a>{<em>template_string</em>} -> <a
href="HTree/Doc.html">HTree::Doc</a>
</li>
</ul>
<p>
Note that the following method, <a href="HTree.html">HTree</a>(), is not a
template method.
</p>
<ul>
<li><a href="HTree.html">HTree</a>(<em>html_string</em>) -> <a
href="HTree/Doc.html">HTree::Doc</a>
</li>
</ul>
<h2>Template Directives.</h2>
<p>
A template directive is described as a special HTML attribute which name
begins with underscore.
</p>
<p>
The template directives are listed as follows.
</p>
<ul>
<li><elem
_attr_<em>name</em>="<em>expr</em>">content</elem>
</li>
<li><elem _text="<em>expr</em>">dummy-content</elem>
</li>
<li><elem _text><em>expr</em></elem>
</li>
<li><elem _tree="<em>expr</em>">dummy-content</elem>
</li>
<li><elem _tree><em>expr</em></elem>
</li>
<li><elem _if="<em>expr</em>"
_else="<em>mod.name(args)</em>">then-content</elem>
</li>
<li><elem
_iter="<em>expr.meth(args)//vars</em>">content</elem>
</li>
<li><elem
_iter_content="<em>expr.meth(args)//vars</em>">content</elem>
</li>
<li><elem
_call="<em>mod.name(args)</em>">dummy-content</elem>
</li>
<li><elem _template="<em>name(vars)</em>">body</elem>
</li>
</ul>
<h3>Template Semantics</h3>
<ul>
<li>attribute substitution
<ul>
<li><elem
_attr_<em>name</em>="<em>expr</em>">content</elem>
</li>
</ul>
<p>
_attr_<em>name</em> is used for a dynamic attribute.
</p>
<pre>
<elem _attr_xxx="..."/>
-> <elem xxx="..."/>
</pre>
<p>
It is expanded to <em>name</em>="content". The content is
generated by evaluating <em>expr</em>. Usually you don‘t need to care
escaping: &, <, > and " are automatically escaped. If you
need to output character references, the value of <em>expr</em> should be
an object which have a <tt>rcdata</tt> method such as an <a
href="HTree/Text.html">HTree::Text</a>. If the value has a <tt>rcdata</tt>
method, it is called and the result is used as the content with escaping
<, > and ".
</p>
<p>
_attr_<em>name</em> can be used multiple times in single element.
</p>
</li>
<li>text substitution
<ul>
<li><elem _text="<em>expr</em>">dummy-content</elem>
</li>
<li><elem _text><em>expr</em></elem>
</li>
</ul>
<p>
_text substitutes the content of the element by the string evaluated from
<em>expr</em>. <em>expr</em> is described in the attribute value or the
content of the element.
</p>
<p>
If a result of <em>expr</em> have &, < and/or >, they are
automatically escaped. If you need to output character references, the
value of <em>expr</em> should be an object which have a <tt>rcdata</tt>
method such as an <a href="HTree/Text.html">HTree::Text</a>. If the value
has a <tt>rcdata</tt> method, it is called and the result is used as the
content with escaping < and >.
</p>
<p>
If the element is span or div, and there is no other attributes, no tags
are produced.
</p>
<pre>
<elem _text="...">dummy-content</elem>
-> <elem>...</elem>
</pre>
</li>
<li>tree substitution
<ul>
<li><elem _tree="<em>expr</em>">dummy-content</elem>
</li>
<li><elem _tree><em>expr</em></elem>
</li>
</ul>
<p>
_tree substitutes the content of the element by the htree object evaluated
from <em>expr</em>. <em>expr</em> is described in the attribute value or
the content of the element.
</p>
<p>
If the element is span or div, and there is no other attributes, no tags
are produced.
</p>
<pre>
<elem _tree="...">dummy-content</elem>
-> <elem>...</elem>
</pre>
</li>
<li>conditional
<ul>
<li><elem _if="<em>expr</em>">then-content</elem>
</li>
<li><elem _if="<em>expr</em>"
_else="<em>name(args)</em>">then-content</elem>
</li>
</ul>
<p>
_if is used for conditional.
</p>
<p>
If <em>expr</em> is evaluated to true, it expands as follows regardless of
existence of _else.
</p>
<pre>
<elem _if="<i>expr</i>">then-content</elem>
-> <elem>then-content</elem>
</pre>
<p>
If <em>expr</em> is evaluated to false, it expands using _else. If _else is
not given, it expands to empty. If _else is given, it expands as follows.
</p>
<pre>
<elem _if="<i>expr</i>" _else="<i>name(args)</i>">then-content</elem>
-> <elem _call="<i>name(args)</i>">then-content</elem>
-> see _call for further expansion.
</pre>
<p>
It is expanded to <elem>then-content</elem> if <em>expr</em> is
evaluated to a true value. Otherwise, it is replaced by other template
specified by _else attribute. If _else attribute is not given, it just
replaced by empty.
</p>
</li>
<li>iteration
<ul>
<li><elem
_iter="<em>expr.meth(args)//vars</em>">content</elem>
</li>
<li><elem
_iter_content="<em>expr.meth(args)//vars</em>">content</elem>
</li>
</ul>
<p>
_iter and _iter_content is used for iteration. _iter iterates the element
itself but _iter_content iterates the content.
</p>
<pre>
<outer _iter="..."><inner/></outer>
-> <outer><inner/></outer><outer><inner/></outer>...
<outer _iter_content="..."><inner/></outer>
-> <outer><inner/><inner/>...</outer>
</pre>
<p>
<em>expr.meth(args)</em> specifies iterator method call. It is actually
called with a block. The block have block parameters <em>vars</em>.
<em>vars</em> must be variables separated by comma.
</p>
</li>
<li>template call
<ul>
<li><elem
_call="<em>name(args)</em>">dummy-content</elem>
</li>
<li><elem
_call="<em>mod.name(args)</em>">dummy-content</elem>
</li>
</ul>
<p>
_call is used to expand a template function. The template function is
defined by _template.
</p>
<pre>
<d _template="m">...</d>
<c _call="m">...</c>
-> <d>...</d>
</pre>
<p>
A local template can be called as follows:
</p>
<pre>
HTree.expand_template{<<'End'}
<a _template=ruby_talk(num)
_attr_href='"http://ruby-talk.org/#{num}"'
>[ruby-talk:<span _text=num>nnn</span>]</a>
Ruby 1.8.0 is released at <span _call=ruby_talk(77946) />.
Ruby 1.8.1 is released at <span _call=ruby_talk(88814) />.
End
</pre>
<p>
<em>mod</em> should be the result of <a
href="HTree.html#M000009">HTree.compile_template</a>.
</p>
<pre>
M = HTree.compile_template(<<'End')
<a _template=ruby_talk(num)
_attr_href='"http://ruby-talk.org/#{num}"'
>[ruby-talk:<span _text=num>nnn</span>]</a>
End
HTree.expand_template{<<'End'}
<html>
Ruby 1.8.0 is released at <span _call=M.ruby_talk(77946) />.
Ruby 1.8.1 is released at <span _call=M.ruby_talk(88814) />.
</html>
End
</pre>
<p>
The module can included. In such case, the template function can be called
without <em>mod.</em> prefix.
</p>
<pre>
include HTree.compile_template(<<'End')
<a _template=ruby_talk(num)
_attr_href='"http://ruby-talk.org/#{num}"'
>[ruby-talk:<span _text=num>nnn</span>]</a>
End
HTree.expand_template{<<'End'}
<html>
Ruby 1.8.0 is released at <span _call=ruby_talk(77946) />.
Ruby 1.8.1 is released at <span _call=ruby_talk(88814) />.
</html>
End
</pre>
</li>
<li>template definition
<ul>
<li><elem _template="<em>name(vars)</em>">body</elem>
</li>
</ul>
<p>
_template defines a template function which is usable by _call.
</p>
<p>
When a template is compiled to a module by <a
href="HTree.html#M000009">HTree.compile_template</a>, the module have a
module function for each template function defined by outermost _template
attribute.
</p>
</li>
</ul>
<h3>White Space Handling</h3>
<p>
The htree template engine strips whitespace text nodes in a template except
under HTML pre element.
</p>
<p>
For example the white space text node between two spans in following
template is stripped.
</p>
<pre>
<span _text="'a'"/> <span _text="'b'"/> -> "ab"
</pre>
<p>
Character entity references are not stripped.
</p>
<pre>
<span _text="'a'"/>&#32;<span _text="'b'"/> -> "a&#32;b"
</pre>
<p>
<a href="HTree/Text.html">Text</a> nodes generated by _text is not
stripped.
</p>
<pre>
<span _text="'a'"/><span _text="' '"> </span><span _text="'b'"/> -> "a b"
</pre>
<h2>HTML and XML</h2>
<p>
The htree template engine outputs HTML or XML.
</p>
<p>
If a template has no XML declaration and the top element is HTML, the
result is HTML. Otherwise the result is XML.
</p>
<p>
They differs as follows.
</p>
<ul>
<li>XML declaration is (re-)generated for XML.
</li>
<li>empty elements ends with a slash for XML.
</li>
<li>script and style element is escaped for XML.
</li>
</ul>
<h2>Design Decision on Design/Logic Separation</h2>
<p>
<a href="HTree.html">HTree</a> template engine doesn‘t force you to
separate design and logic. Any logic (Ruby code) can be embedded in design
(HTML).
</p>
<p>
However the template engine cares the separation by logic refactorings. The
logic is easy to move between a template and an application. For example,
following tangled template
</p>
<pre>
tmpl.html:
<html>
<head>
<title _text="very-complex-ruby-code">dummy</title>
</head>
...
</html>
app.rb:
HTree.expand_template('tmpl.html', obj)
</pre>
<p>
can be refactored as follows.
</p>
<pre>
tmpl.html:
<html>
<head>
<title _text="title">dummy</title>
</head>
...
</html>
app.rb:
def obj.title
very-complex-ruby-code
end
HTree.expand_template('tmpl.html', obj)
</pre>
<p>
In general, any expression in a template can be refactored to an
application by extracting it as a method. In JSP, this is difficult
especially for a code fragment of an iteration.
</p>
<p>
Also <a href="HTree.html">HTree</a> encourages to separate business logic
(Ruby code in an application) and presentation logic (Ruby code in a
template). For example, presentation logic to color table rows stripe can
be embedded in a template. It doesn‘t need to tangle an application.
</p>
</div>
</div>
<div id="method-list">
<h3 class="section-bar">Methods</h3>
<div class="name-list">
<a href="#M000003">==</a>
<a href="#M000009">compile_template</a>
<a href="#M000004">eql?</a>
<a href="#M000008">expand_template</a>
<a href="#M000005">hash</a>
<a href="#M000006">parse</a>
<a href="#M000007">parse_xml</a>
</div>
</div>
</div>
<!-- if includes -->
<div id="section">
<div id="class-list">
<h3 class="section-bar">Classes and Modules</h3>
Module <a href="HTree/Container.html" class="link">HTree::Container</a><br />
Module <a href="HTree/Leaf.html" class="link">HTree::Leaf</a><br />
Module <a href="HTree/Node.html" class="link">HTree::Node</a><br />
Module <a href="HTree/Traverse.html" class="link">HTree::Traverse</a><br />
Class <a href="HTree/BogusETag.html" class="link">HTree::BogusETag</a><br />
Class <a href="HTree/Comment.html" class="link">HTree::Comment</a><br />
Class <a href="HTree/Context.html" class="link">HTree::Context</a><br />
Class <a href="HTree/Doc.html" class="link">HTree::Doc</a><br />
Class <a href="HTree/DocType.html" class="link">HTree::DocType</a><br />
Class <a href="HTree/Elem.html" class="link">HTree::Elem</a><br />
Class <a href="HTree/Encoder.html" class="link">HTree::Encoder</a><br />
Class <a href="HTree/Error.html" class="link">HTree::Error</a><br />
Class <a href="HTree/Location.html" class="link">HTree::Location</a><br />
Class <a href="HTree/Name.html" class="link">HTree::Name</a><br />
Class <a href="HTree/ProcIns.html" class="link">HTree::ProcIns</a><br />
Class <a href="HTree/Text.html" class="link">HTree::Text</a><br />
Class <a href="HTree/XMLDecl.html" class="link">HTree::XMLDecl</a><br />
</div>
<!-- if method_list -->
<div id="methods">
<h3 class="section-bar">Public Class methods</h3>
<div id="method-M000009" class="method-detail">
<a name="M000009"></a>
<div class="method-heading">
<a href="HTree.src/M000009.html" target="Code" class="method-signature"
onclick="popupCode('HTree.src/M000009.html');return false;">
<span class="method-name">HTree.compile_template(template_string) → module<br />
</span>
</a>
</div>
<div class="method-description">
<p>
<tt><a
href="HTree.html#M000009">HTree.compile_template</a>(<em>template_string</em>)</tt>
compiles <em>template_string</em> as a template.
</p>
<p>
<a href="HTree.html#M000009">HTree.compile_template</a> returns a module.
The module has module functions for each templates defined in
<em>template_string</em>. The returned module can be used for
<tt>include</tt>.
</p>
<pre>
M = HTree.compile_template(<<'End')
<p _template=birthday(subj,t)>
<span _text=subj />'s birthday is <span _text="t.strftime('%B %dth %Y')"/>.
</p>
End
M.birthday('Ruby', Time.utc(1993, 2, 24)).display_xml
# <p>Ruby's birthday is February 24th 1993.</p>
</pre>
<p>
The module function takes arguments specifies by a <tt>_template</tt>
attribute and returns a tree represented as <a
href="HTree/Node.html">HTree::Node</a>.
</p>
</div>
</div>
<div id="method-M000008" class="method-detail">
<a name="M000008"></a>
<div class="method-heading">
<a href="HTree.src/M000008.html" target="Code" class="method-signature"
onclick="popupCode('HTree.src/M000008.html');return false;">
<span class="method-name">HTree.expand_template(template_pathname, obj=Object.new, out=$stdout, encoding=internal_encoding) → out<br />
HTree.expand_template(out=$stdout, encoding=internal_encoding) { template_string } → out<br />
</span>
</a>
</div>
<div class="method-description">
<p>
<tt><a href="HTree.html#M000008">HTree.expand_template</a></tt> expands a
template.
</p>
<p>
The arguments should be specified as follows. All argument except
<em>pathname</em> are optional.
</p>
<ul>
<li><a href="HTree.html#M000008">HTree.expand_template</a>(<em>pathname</em>,
<em>obj</em>, <em>out</em>, <em>encoding</em>) -> <em>out</em>
</li>
<li><a href="HTree.html#M000008">HTree.expand_template</a>(<em>out</em>,
<em>encoding</em>) {<em>template_string</em>} -> <em>out</em>
</li>
</ul>
<p>
The template is specified by a file or a string. If a block is not given,
the first argument represent a template pathname. Otherwise, the block is
yielded and its value is interpreted as a template string. So it can be
called as follows in simplest case.
</p>
<ul>
<li><a
href="HTree.html#M000008">HTree.expand_template</a>(<em>template_pathname</em>)
</li>
<li><a
href="HTree.html#M000008">HTree.expand_template</a>{<em>template_string</em>}
</li>
</ul>
<p>
Ruby expressions in the template file specified by
<em>template_pathname</em> are evaluated in the context of the optional
second argument <em>obj</em> as follows. I.e. the pseudo variable self in
the expressions is bound to <em>obj</em>.
</p>
<pre>
HTree.expand_template(template_pathname, obj)
</pre>
<p>
Ruby expressions in the template_string are evaluated in the context of the
caller of <a href="HTree.html#M000008">HTree.expand_template</a>. (binding
information is specified by the block.) I.e. they can access local
variables etc. We recommend to specify template_string as a literal string
without interpolation because dynamically generated string may break
lexical scope.
</p>
<p>
<a href="HTree.html#M000008">HTree.expand_template</a> has two more
optional arguments: <em>out</em>, <em>encoding</em>.
</p>
<p>
<em>out</em> specifies output target. It should have <tt><<</tt>
method: IO and String for example. If it is not specified, $stdout is used.
If it has a method <tt>charset=</tt>, it is called to set the minimal
charset of the result before <tt><<</tt> is called.
</p>
<p>
<em>encoding</em> specifies output character encoding. If it is not
specified, internal encoding is used.
</p>
<p>
<a href="HTree.html#M000008">HTree.expand_template</a> returns <em>out</em>
or $stdout if <em>out</em> is not specified.
</p>
</div>
</div>
<div id="method-M000006" class="method-detail">
<a name="M000006"></a>
<div class="method-heading">
<a href="HTree.src/M000006.html" target="Code" class="method-signature"
onclick="popupCode('HTree.src/M000006.html');return false;">
<span class="method-name">parse</span><span class="method-args">(input)</span>
</a>
</div>
<div class="method-description">
<p>
<a href="HTree.html#M000006">HTree.parse</a> parses <em>input</em> and
return a document tree. represented by <a
href="HTree/Doc.html">HTree::Doc</a>.
</p>
<p>
<em>input</em> should be a String or an object which respond to read or
open method. For example, IO, StringIO, Pathname, URI::HTTP and URI::FTP
are acceptable. Note that the URIs need open-uri.
</p>
<p>
<a href="HTree.html#M000006">HTree.parse</a> guesses <em>input</em> is HTML
or not and XML or not.
</p>
<p>
If it is guessed as HTML, the default namespace in the result is set to <a
href="http://www.w3.org/1999/xhtml">www.w3.org/1999/xhtml</a> regardless of
<em>input</em> has XML namespace declaration or not nor even it is pre-XML
HTML.
</p>
<p>
If it is guessed as HTML and not XML, all element and attribute names are
downcaseed.
</p>
<p>
If opened file or read content has charset method, <a
href="HTree.html#M000006">HTree.parse</a> decode it according to $KCODE
before parsing. Otherwise <a href="HTree.html#M000006">HTree.parse</a>
assumes the character encoding of the content is compatible to $KCODE. Note
that the charset method is provided by URI::HTTP with open-uri.
</p>
</div>
</div>
<div id="method-M000007" class="method-detail">
<a name="M000007"></a>
<div class="method-heading">
<a href="HTree.src/M000007.html" target="Code" class="method-signature"
onclick="popupCode('HTree.src/M000007.html');return false;">
<span class="method-name">parse_xml</span><span class="method-args">(input)</span>
</a>
</div>
<div class="method-description">
<p>
<a href="HTree.html#M000007">HTree.parse_xml</a> parses <em>input</em> as
XML and return a document tree represented by <a
href="HTree/Doc.html">HTree::Doc</a>.
</p>
<p>
It behaves almost same as <a href="HTree.html#M000006">HTree.parse</a> but
it assumes <em>input</em> is XML even if no XML declaration. The assumption
causes following differences.
</p>
<ul>
<li>doesn‘t downcase element name.
</li>
<li>The content of <script> and <style> element is PCDATA, not
CDATA.
</li>
</ul>
</div>
</div>
<h3 class="section-bar">Public Instance methods</h3>
<div id="method-M000003" class="method-detail">
<a name="M000003"></a>
<div class="method-heading">
<a href="HTree.src/M000003.html" target="Code" class="method-signature"
onclick="popupCode('HTree.src/M000003.html');return false;">
<span class="method-name">==</span><span class="method-args">(other)</span>
</a>
</div>
<div class="method-description">
<p>
compare tree structures.
</p>
</div>
</div>
<div id="method-M000004" class="method-detail">
<a name="M000004"></a>
<div class="method-heading">
<span class="method-name">eql?</span><span class="method-args">(other)</span>
</div>
<div class="method-description">
<p>
Alias for #==
</p>
</div>
</div>
<div id="method-M000005" class="method-detail">
<a name="M000005"></a>
<div class="method-heading">
<a href="HTree.src/M000005.html" target="Code" class="method-signature"
onclick="popupCode('HTree.src/M000005.html');return false;">
<span class="method-name">hash</span><span class="method-args">()</span>
</a>
</div>
<div class="method-description">
<p>
<a href="HTree.html#M000005">hash</a> value for the tree structure.
</p>
</div>
</div>
</div>
</div>
<div id="validator-badges">
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>
</body>
</html>
|