Fragments

Fragments may also contain snippets of Ruby. This is the programming language used to produce Fragmenta, and can be used to produce shortcuts, insert HTML or iterate collections of other fragments for info.

Ruby formatting

When embedding Ruby inside fragments, you use eruby formatting. Note that you can also do this in fields (though not a date field). This means you enclose the ruby snippet with angle brackets and percentage signs, usually also adding an ’=’ which means print the result to the page.

So for example

<%= Time.now %>

produces :

Thu Jun 17 16:44:25 +0100 2010

Obviously this is a trivial example, but being able to embed ruby in fragments means that you can produce anything you like on the pages, including data about the system, file listings etc. As this code is run only once on your local machine when generating the web pages, there are no security implications as there would be with a script on the server.

You can also call any functions defined in the page or fragment controllers – for example this help file uses the call

<%= g 'eruby' %>

which in turn calls the function ‘g’ inserted into the page controller file at Style/controllers/page.rb (simply a shortcut for the link tag) :


# Shortcut for google link
#
#
def g(text)
  link(:google => text)
end

and produces the google link

eruby

To call fragment functions, use

<%= @fragment.function_name() %>

inside a fragment’s text. For a full list of functions, see Templates.