Module: ScrivitoHelper
- Includes:
- Scrivito::ControllerHelper
- Defined in:
- app/helpers/scrivito_helper.rb
Overview
This module provides several helper methods for rendering the CMS contents and enabling the in-place editing.
Details Dialog Size collapse
-
#scrivito_large_dialog(&block) ⇒ String
Set the size of the page and widget details dialog to
large. -
#scrivito_medium_dialog(&block) ⇒ String
Set the size of the page and widget details dialog to
medium(default). -
#scrivito_small_dialog(&block) ⇒ String
Set the size of the page and widget details dialog to
small.
Instance Method Summary collapse
-
#scrivito_body_tags ⇒ Object
Renders all tags needed in the HTML body.
-
#scrivito_cache(key, options = nil, &block) ⇒ Object
This method wraps Rails’ built-in fragment caching to work with content delivered by Scrivito.
-
#scrivito_details_for(title = nil, &block) ⇒ Object
Attribute group helper generates HTML for page details dialog and widget details dialog.
-
#scrivito_field(obj, field_name) ⇒ Object
Renders a field from the CMS.
-
#scrivito_head_tags ⇒ Object
Renders all tags needed in the HTML head.
-
#scrivito_image_tag(obj, field_name_or_tag_options = nil, tag_or_editing_options = {}, editing_options = {}) ⇒ String
Calculates an HTML image tag of an image stored in the CMS for inplace editing.
-
#scrivito_tag(tag_name, obj_or_widget, field_name, html_options = {}, editing_options = {}, &block) ⇒ String
Renders a field within the given HTML tag.
-
#scrivito_tag_list(tag_name, obj, method_name, options = {}) {|list, child| ... } ⇒ String
Renders a navigation ready for in-place editing.
-
#scrivito_thumbnail(title, icon = :scrivito, &block) ⇒ Object
Thumbnail helper generates HTML for the page class selection dialog and the widget class selection dialog.
-
#scrivito_value(value) ⇒ Object
Renders an attribute value of a CMS model, taking its type into account.
Methods included from Scrivito::ControllerHelper
#scrivito_in_editable_view?, #scrivito_path, #scrivito_url, #scrivito_user
Instance Method Details
#scrivito_body_tags ⇒ Object
Renders all tags needed in the HTML body.
395 396 397 |
# File 'app/helpers/scrivito_helper.rb', line 395 def Scrivito::LayoutTags.new(self).page_config(@obj) end |
#scrivito_cache(key, options = nil, &block) ⇒ Object
This method wraps Rails’ built-in fragment caching to work with content delivered by Scrivito. Fragment caching applies to computed parts of views and helps to improve performance. The Rails guides provide an excellent introduction to caching if you haven’t used it yet.
The scrivito_cache method extends built-in fragment caching so that cached parts of a view are automatically recomputed when Scrivito content changes. The fragments are only cached for the published content. In editable working copies no caching takes place, and the fragments are computed for every request.
442 443 444 445 |
# File 'app/helpers/scrivito_helper.rb', line 442 def scrivito_cache(key, =nil, &block) workspace = Scrivito::Workspace.current cache_if(workspace.published?, [workspace.cache_key, key], , &block) end |
#scrivito_details_for(title = nil, &block) ⇒ Object
Attribute group helper generates HTML for page details dialog and widget details dialog. The generated HTML has appropriate DOM structure and CSS classes, which are compatible with the CSS of the SDK. By using this helper you ensure, that the look of your attribute groups fits into the SDK’s design.
325 326 327 328 329 330 331 332 333 334 |
# File 'app/helpers/scrivito_helper.rb', line 325 def scrivito_details_for(title = nil, &block) content_tag(:div, class: 'scrivito_content_group') do capture do if title concat content_tag(:h3, title) end concat capture(&block) end end end |
#scrivito_field(obj, field_name) ⇒ Object
Content rendered using this method will not be editable in the Scrivito UI. If you want in-place editing, then please use #scrivito_tag instead.
Renders a field from the CMS.
250 251 252 |
# File 'app/helpers/scrivito_helper.rb', line 250 def scrivito_field(obj, field_name) scrivito_value(obj[field_name]) end |
#scrivito_head_tags ⇒ Object
Renders all tags needed in the HTML head.
383 384 385 386 387 388 389 |
# File 'app/helpers/scrivito_helper.rb', line 383 def = Scrivito::LayoutTags.new(self) capture do concat .editing_auth_warning concat . end end |
#scrivito_image_tag(obj, field_name_or_tag_options = nil, tag_or_editing_options = {}, editing_options = {}) ⇒ String
If you do not specify an HTML alt attribute, the helper method will use Obj#alt_description of the target object.
Calculates an HTML image tag of an image stored in the CMS for inplace editing.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/helpers/scrivito_helper.rb', line 185 def scrivito_image_tag(obj, =nil, = {}, = {}) field_name, , = if .is_a?(Hash) [nil, , ] else [, , ] end if field_name.blank? if obj.binary? field_name = :blob else raise Scrivito::ScrivitoError, "when omitting `field_name' you have to pass a binary obj" end end = Scrivito::ImageTag.new(self).(obj, field_name, .with_indifferent_access, .with_indifferent_access) scrivito_tag('img', obj, field_name, ) end |
#scrivito_large_dialog(&block) ⇒ String
Set the size of the page and widget details dialog to large.
349 350 351 |
# File 'app/helpers/scrivito_helper.rb', line 349 def scrivito_large_dialog(&block) Scrivito::DialogSizeHelper.render_dialog_with_size(self, 'large', &block) end |
#scrivito_medium_dialog(&block) ⇒ String
Set the size of the page and widget details dialog to medium (default).
361 362 363 |
# File 'app/helpers/scrivito_helper.rb', line 361 def scrivito_medium_dialog(&block) Scrivito::DialogSizeHelper.render_dialog_with_size(self, 'medium', &block) end |
#scrivito_small_dialog(&block) ⇒ String
Set the size of the page and widget details dialog to small.
373 374 375 |
# File 'app/helpers/scrivito_helper.rb', line 373 def scrivito_small_dialog(&block) Scrivito::DialogSizeHelper.render_dialog_with_size(self, 'small', &block) end |
#scrivito_tag(tag_name, obj_or_widget, field_name, html_options = {}, editing_options = {}, &block) ⇒ String
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.
70 71 72 73 74 75 76 |
# File 'app/helpers/scrivito_helper.rb', line 70 def scrivito_tag(tag_name, , field_name, = {}, = {}, &block) Scrivito::CmsFieldTag.new(self, tag_name, , .merge( widget_render_context: , field_name: field_name.to_s )).render(, &block) end |
#scrivito_tag_list(tag_name, obj, method_name, options = {}) {|list, child| ... } ⇒ String
Renders a navigation ready for in-place editing.
If a navigation is rendered using this helper method, a special menu is attached to it that lets you change the order of the child pages or insert a new child.
For making the child pages sortable, the parent Obj requires a referencelist attribute named child_order. When creating a page model using the page generator, such an attribute is automatically added to the model.
134 135 136 |
# File 'app/helpers/scrivito_helper.rb', line 134 def scrivito_tag_list(tag_name, obj, method_name, = {}, &block) Scrivito::ChildListTag.new(tag_name, obj, method_name.to_s, self).render(, &block) end |
#scrivito_thumbnail(title, icon = :scrivito, &block) ⇒ Object
Thumbnail helper generates HTML for the page class selection dialog and the widget class selection dialog. The generated HTML has appropriate DOM structure and CSS classes, which are compatible with the CSS of the SDK. By using this helper you ensure, that the look of your thumbnails fits into the SDK’s design.
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'app/helpers/scrivito_helper.rb', line 284 def scrivito_thumbnail(title, icon = :scrivito, &block) if icon.is_a?(Symbol) icon_code = { content: '', headline: '', image: '', scrivito: '', text: '', }.fetch(icon, '') icon = content_tag(:i, icon_code.html_safe, class: 'scrivito_icon') end content_tag(:div, class: 'scrivito_editing_widget_preview') do capture do concat content_tag(:div, icon, class: 'scrivito_editing_widget_visualization') concat content_tag(:div, title, class: 'scrivito_editing_widget_title') concat content_tag(:div, class: 'scrivito_editing_widget_description', &block) end end end |
#scrivito_value(value) ⇒ Object
Content rendered using this method will not be editable in the Scrivito UI. If you want in-place editing, use #scrivito_tag instead.
Renders an attribute value of a CMS model, taking its type into account.
Links inside html attributes are rendered using the routing of the application. Values from string attributes are escaped. For other attribute types, a simple default representation is rendered.
223 224 225 226 227 228 229 230 231 232 233 |
# File 'app/helpers/scrivito_helper.rb', line 223 def scrivito_value(value) renderer = Scrivito::AttributeValueRenderer.new(self) case value when Scrivito::BasicWidget then render(template: value.show_view_path, locals: {widget: value}) when Scrivito::HtmlString then renderer.render_as_html(value) when String then renderer.render_as_string(value) when Time then renderer.render_as_date(value) else value end end |