The code below is auto-generated. Don‘t edit manually.
DefaultContext | = | Context.new |
HTMLContext | = | DefaultContext.subst_namespaces(nil=>"http://www.w3.org/1999/xhtml") |
HTree.compile_template(template_string) compiles template_string as a template.
HTree.compile_template returns a module. The module has module functions for each templates defined in template_string. The returned module can be used for include.
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 xmlns="http://www.w3.org/1999/xhtml">Ruby's birthday is February 24th 1993.</p>
The module function takes arguments specifies by a _template attribute and returns a tree represented as HTree::Node.
HTree.expand_template expands a template.
The arguments should be specified as follows. All argument except pathname are optional.
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.
Ruby expressions in the template file specified by template_pathname are evaluated in the context of the optional second argument obj as follows. I.e. the pseudo variable self in the expressions is bound to obj.
HTree.expand_template(template_pathname, obj)
Ruby expressions in the template_string are evaluated in the context of the caller of HTree.expand_template. (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.
HTree.expand_template has two more optional arguments: out, encoding.
out specifies output target. It should have << method: IO and String for example. If it is not specified, $stdout is used.
encoding specifies output character encoding. If it is not specified, internal encoding is used.
HTree.expand_template returns out or $stdout if out is not specified.
HTree.parse parses input and return a document tree. represented by HTree::Doc.
input 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.
HTree.parse guesses input is HTML or not. If it is guessed as HTML, the default namespace in the result is set to www.w3.org/1999/xhtml regardless of input has XML namespace declaration or not nor even it is pre-XML HTML.
If opened file or read content has charset method, HTree.parse decode it according to $KCODE before parsing. Otherwise HTree.parse assumes the character encoding of the content is compatible to $KCODE. Note that the charset method is provided by URI::HTTP with open-uri.
HTree.parse_xml parses input as XML and return a document tree represented by HTree::Doc.
It behaves almost same as HTree.parse but it assumes <i>input</> is XML even if no XML declaration. The assumption causes following differences.