Method: ScrivitoHelper#scrivito_tag

Defined in:
app/helpers/scrivito_helper.rb

#scrivito_tag(tag_name, obj_or_widget, field_name, html_options = {}, editing_options = {}, &block) ⇒ String

Note:

If the param field_name is of type widget, then tag_name must be a block tag, like div or h1. An inline tag like p or span could result in broken HTML output, since the widgets are rendered within block tags.

Renders a field within the given HTML tag.

This method also renders additional attributes, which are needed for in-place editing. These attributes are only rendered when appropriate, i.e. not for a regular visitor.

The helper is similar to (and internally uses) api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag. You can add additional HTML attributes by passing them in html_options.

Examples:

Renders an <h2> tag containing the text of the headline attribute of @obj and assigns the tag a css class called very_important

scrivito_tag(:h2, @obj, :headline, class: "very_important")

Renders an <h2> tag containing a truncated headline of the widget

scrivito_tag(:h2, widget, :headline) do
  truncate(widget.headline)
end

Render a download link for a PDF document

scrivito_tag(:div, @obj, :pdf) do
  if @obj.pdf
    "Download: #{link_to(@obj.pdf.filename, @obj.pdf.url)}"
  elsif scrivito_user
    "Drop PDF here to upload"
  end
end

Render widgetlist inside an ul tag with the individual widgets inside of li tags.

scrivito_tag(:ul, @obj, :body, {}, inner_tag: :li)

Parameters:

  • tag_name (String, Symbol)

    Name of the HTML tag (e.g. :h1 or :div). If the param field_name is of type widget, then tag_name must be a block tag, like div or h1. An inline tag like p or span could result in broken HTML output, since the widgets are rendered within block tags.

  • obj_or_widget (Scrivito::BasicObj, Scrivito::BasicWidget)

    A Scrivito::BasicObj or Scrivito::BasicWidget from which the field_name is read.

  • field_name (String, Symbol)

    Which field of the Obj should be rendered.

  • html_options (Hash) (defaults to: {})

    HTML options to be passed to content_tag.

  • editing_options (Hash) (defaults to: {})

    Additional editing options for widgets (e.g. :inner_tag)

  • block (Proc)

    Optional block to render inner HTML. If none given the value of attribute will be rendered instead. block is not allowed for fields of type widget.

Options Hash (editing_options):

  • :inner_tag (Symbol)

    Wraps widgets inside specified tag

  • :editor (String, Symbol)

    Name of the JavaScript editor to be used for this field. Normally, the name of the editor to be used for a field is determined by the scrivito.select_editor JavaScript API. The option :editor should only be used if setting the editor via the JavaScript API is not feasible. Specifying editor: false disables in-place editing.

  • :disable_margins (Boolean)

    If true, no margins are attached to the widgets contained in a widgetlist attribute. In this case, the in-place editing controls of widgets may overlap which can be handled by using suitable CSS. Defaults to false.

Returns:

  • (String)

    The rendered HTML tag.

Raises:

  • ArgumentError if the field behind field_name is of type widget and a block is given.



73
74
75
76
77
78
79
# File 'app/helpers/scrivito_helper.rb', line 73

def scrivito_tag(tag_name, obj_or_widget, field_name,
                 html_options = {}, editing_options = {}, &block)
  Scrivito::CmsFieldTag.new(self, tag_name, obj_or_widget, editing_options.merge(
    widget_render_context: @scrivito_widget_render_context,
    field_name: field_name.to_s
  )).render(html_options, &block)
end