The Slices field is used to define a dynamic zone for richer page layouts.
Here is a simple example that shows how to add slices to your templates. In this example, we have two slice options: a text slice and an image gallery slice.
The "text" slice is simple and only contains one field, which is non-repeatable.
Property | Description |
---|---|
non-repeatable |
- A Rich Text field with the API ID of "rich_text" |
repeatable |
None |
The "image_gallery" slice contains both repeatable and non-repeatable fields.
Property | Description |
---|---|
non-repeatable |
- A Title field with the API ID of "gallery_title" |
repeatable |
- An Image field with the API ID of "gallery_image" |
Here is an example of how to integrate these slices into a blog post.
<div class="blog-content">
<% @document["blog_post.body"].slices.each do |slice| %>
<% case slice.slice_type
when "text" %>
<%= slice.non_repeat["rich_text"].as_html(link_resolver).html_safe %>
<% when "image_gallery" %>
<h2><%= slice.non_repeat["gallery_title"].as_text %></h2>
<% slice.repeat.each do |image| %>
<img src="<%= image["gallery_image"].url %>">
<% end %>
<% end %>
<% end %>
</div>
The following is a more advanced example that shows how to use Slices for a landing page. In this example, the Slice choices are FAQ question/answers, featured items, and text sections.
The "faq" slice is takes advantage of both the repeatable and non-repeatable slice sections.
Property | Description |
---|---|
non-repeatable |
- A Title field with the API ID of "faq_title" |
repeatable |
- A Title field with the API ID of "question" - A Rich Text field with the API ID of "answer" |
The "featured_items" slice contains a repeatable set of an image, title, and summary fields.
Property | Description |
---|---|
non-repeatable |
None |
repeatable |
- An Image field with the API ID of "image" - A Title field with the API ID of "title" - A Rich Text field with the API ID of "summary" |
The "text" slice contains only a Rich Text field in the non-repeatable section.
Property | Description |
---|---|
non-repeatable |
- A Rich Text field with the API ID of "rich_text" |
repeatable |
None |
Here is an example of how to integrate these slices into a landing page.
<div class="page-content">
<% @document["page.body"].slices.each do |slice| %>
<% case slice.slice_type
when "faq" %>
<div class="faq">
<%= slice.non_repeat["faq_title"].as_html(link_resolver).html_safe %>
<% slice.repeat.each do |faq| %>
<div>
<%= faq["question"].as_html(link_resolver).html_safe %>
<%= faq["answer"].as_html(link_resolver).html_safe %>
</div>
<% end %>
</div>
<% when "featured_items" %>
<div class="featured-items">
<% slice.repeat.each do |featured_item| %>
<% image_url = featured_item["image"].url %>
<div>
<img src="<%= image_url %>">
<%= featured_item["title"].as_html(link_resolver).html_safe %>
<%= featured_item["summary"].as_html(link_resolver).html_safe %>
</div>
<% end %>
</div>
<% when "text" %>
<div class="text">
<%= slice.non_repeat["rich_text"].as_html(link_resolver).html_safe %>
</div>
<% end %>
<% end %>
</div>