Customizing a Widget

The Scrivito example app is a collection of some of the most used pages and widgets available with Scrivito integration. Some customers even use the example app as written and just update the content to fit their business. While the example app is designed to show you the many options available with Scrivito, they are not the only options. The flexibility of Scrivito allows you to customize just about every part to fit your needs. Add, remove, write your own... Scrivito was designed to put you in control, not to be in the way.

Recently we were asked how to add an image to the heading of a card widget. We thought this was a really nice option, was pretty easy to implement and shows just how flexible and easy Scrivito can be.  

The card widget already has many options. For example, you can update the color of the heading or add an image to the body of the card. The default options are well displayed in the example app. But, let's customize it further.

Using the example app code as a reference, here is what you would add to have an image in the heading of a card widget.

The card widget lets you present content in a stylish manner.

First, we want to add another attribute to the model. The heading_image attribute is of the reference type and serves as a pointer to the image to use.

# app/models/card_widget.rb ... attribute :heading_image, :reference ...

Next, we want to add the option to select an image for the heading in the widget properties:

# app/views/card_widget/details.html.erb ... <%= scrivito_details_for("Heading Background Image") do %>   <%= scrivito_tag(:div, widget, :heading_image, data: {     scrivito_editors_filter_context: {       _image: true,     },   }) %> <% end %> ...

The scrivito_tag helper not only displays the image on the properties view but also lets you select one in editing mode using the Content Browser.

Finally, on the actual pages containing this widget, we want to add the image to the already existing “panel heading” div and show.heading “if” statement: 

# app/views/card_widget/show.html.erb ... <% if widget.show_heading? %>   <div class="panel-heading"     <% if widget.heading_image.present? %>       data-background-image-url="<%= scrivito_url(widget.heading_image) %>"     <% end %>     >     <%= scrivito_tag(:h3, widget, :title, class: "panel-title") %>   </div> <% end %> ...

That is it. Now, if an editor clicks the handle of a card widget (at the upper right corner) and selects “Widget properties” they will have the option to add a "Heading Background Image". 

Go from this... this in three easy steps.

Let us know what customizations you want. We are happy to work with you to make it happen.