Scripting

You can embed Ruby script blocks in Rextile documents (using erb). Simply surround them with <%...%> tags. A script block like <%=...%> inserts its result at the point of the block, a block without the = inserts nothing. All these script blocks are run before the text is converted from Textile to HTML.

Script blocks within the XHTML wrapper can access the Hpricot DOM of the XHTML document—as built so far—in html_doc. This DOM is read only. But you can, for example, query it using REXML::XPath to build a table of contents.

If you want to defer a script block within the Rextile source until the XHTML processing phase – to get at the DOM, for example – you have replace the % with a ยง in the script block delimiters.

Script Nodes

You can also emit script nodes into the XHTML. These are processed at the very end and they may modify the DOM. They know where in the DOM they are by reading html_script_node. A script node is something you typically emit from a helper Rextile script function, such as:

def toc( from = 2, to = 4 )
    "<pre class=\"rscript\">xtoc #{from}, #{to}</pre>" 
end

The script node calls another, deferred function, which can then access the DOM. See the xtoc() function in templates/standard/_globals.rb for a complete example.