module Scrivito # This module contains a helper that can be used to render attributes of the CMS models. # @api public module DisplayHelper # For a consistent look of your site and easy maintenance, only the display helpers # should be used to render an attribute value of a CMS model. # # <%= display_value @obj.title %> # Renders the value, taking its type into account. # * An HtmlString will be exported by converting its links # * A Time will be exported in an international form: Year-Month-Day Hour:Minutes # * A non-HTML String will be quoted # * other values will be rendered unchanged # # @api public def display_value(value) case value when HtmlString then convert_links(value).html_safe when String then h(value) when Time then h(l(value)) else value end end # Renders a field from the CMS. # # <%= display_field @obj, :title %> # # @api public def display_field(obj, attr, options = {}) display_value obj[attr] end # Legacy method - deprecated # # Use display_value and display_field instead def display(*args) if args.last.kind_of? Hash options = args.pop else options = {} end return display_value(args.first) if args.size == 1 display_field(args[0], args[1], options) end private def convert_links(html, cms_path_options = {}) if html html.gsub(%r{?}) do if obj = Obj.find_by_id($1) cms_path(obj, cms_path_options) else "#__target_object_not_reachable" end end end end end end