Delivering CMS Content Using ERB Templates

The CMS controller of the  Rails Connector uses views to deliver the contents of documents and folders. By means of URL routing, exactly one CMS file becomes the current context. In the view, this context can be accessed via the @obj instance variable of Obj class.

An ERB template is a view consisting of HTML code with embedded Ruby commands. The latter are used to generate content dynamically. To output, for example, the name of the current CMS file, you can use:

<%= @obj.name %>

The version fields of a file can be accessed in the same way. There are two variants for writing out a field such as the title of the released version (if no released version exists both commands lead to an error):

<%= @obj.title %>
<%= @obj[:title] %>

Use @obj.title if you wish to generate an error if the referenced field is missing. If missing fields are to be ignored, use @obj[:title].

We recommend using the display_field helper for having all values displayed correctly (up to version 6.6.1 the helper display is used instead of display_field and display_value). Use it as follows:

<%= display_field @obj, :title %>

This command automatically places an edit marker for the referenced field into the preview. Use one of the following notations to disable this feature (up to version 6.6.1 the helper display is used instead of display_field and display_value):

<%= display_value @obj.title %>
<%= display_field @obj, :title, :marker => false %>

More complex functions such as creating a navigation are normally not implemented in the views directly. For clarity, better maintenance, and reusability, so-called helpers are used for this.

For HTML fragments to be reused, the Rails framework provides the concept of partials – small templates that can be stored separately.