Method: HTree.compile_template

Defined in:
lib/htree/template.rb

.compile_template(template_string) ⇒ Object

call-seq:

HTree.compile_template(template_string) -> module

HTree.compile_template(<i>template_string</i>) 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("<p _template=birthday(subj,t)>\n  <span _text=subj />'s birthday is <span _text=\"t.strftime('%B %dth %Y')\"/>.\n</p>\n")
M.birthday('Ruby', Time.utc(1993, 2, 24)).display_xml
# <p>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.



417
418
419
420
421
422
423
424
425
426
# File 'lib/htree/template.rb', line 417

def HTree.compile_template(template_string)
  code = HTree::TemplateCompiler.new.compile_template(template_string)
  Thread.current[:htree_compile_template_code] = code
  mod = eval("      eval(Thread.current[:htree_compile_template_code])\n    End\n    HTree::EmptyBindingObject.empty_binding, \"(eval:\#{__FILE__}:\#{__LINE__})\")\n  Thread.current[:htree_compile_template_code] = nil\n  mod\nend\n",