Module: Alchemy::ElementsHelper
- Includes:
- ElementsBlockHelper, UrlHelper
- Included in:
- PagesHelper
- Defined in:
- app/helpers/alchemy/elements_helper.rb
Overview
This helpers are useful to render elements from pages.
The most important helper for frontend developers is the #render_elements helper.
Instance Method Summary collapse
-
#element_preview_code(element) ⇒ Object
Renders the HTML tag attributes required for preview mode.
-
#element_preview_code_attributes(element) ⇒ Object
Returns a hash containing the HTML tag attributes required for preview mode.
-
#element_tags(element, options = {}) ⇒ String
Returns the element’s tags information as a string.
-
#element_tags_attributes(element, options = {}) ⇒ Hash
Returns the element’s tags information as an attribute hash.
-
#render_element(element, options = {}, counter = 1) ⇒ Object
This helper renders a Element view partial.
-
#render_elements(options = {}, &blk) ⇒ Object
Renders elements from given page.
Methods included from ElementsBlockHelper
Methods included from UrlHelper
#download_alchemy_attachment_path, #download_alchemy_attachment_url, #show_alchemy_page_path, #show_alchemy_page_url, #show_page_path_params
Instance Method Details
#element_preview_code(element) ⇒ Object
Renders the HTML tag attributes required for preview mode.
170 171 172 |
# File 'app/helpers/alchemy/elements_helper.rb', line 170 def element_preview_code(element) tag_builder.(element_preview_code_attributes(element)) end |
#element_preview_code_attributes(element) ⇒ Object
Returns a hash containing the HTML tag attributes required for preview mode.
175 176 177 178 179 |
# File 'app/helpers/alchemy/elements_helper.rb', line 175 def element_preview_code_attributes(element) return {} unless element.present? && Current.preview_page?(element.page) {"data-alchemy-element" => element.id} end |
#element_tags(element, options = {}) ⇒ String
Returns the element’s tags information as a string. Parameters and options are equivalent to #element_tags_attributes.
189 190 191 |
# File 'app/helpers/alchemy/elements_helper.rb', line 189 def (element, = {}) tag_builder.((element, )) end |
#element_tags_attributes(element, options = {}) ⇒ Hash
Returns the element’s tags information as an attribute hash.
204 205 206 207 208 209 210 211 212 |
# File 'app/helpers/alchemy/elements_helper.rb', line 204 def (element, = {}) = { formatter: lambda { || .join(" ") } }.merge() return {} if !element.taggable? || element.tag_list.blank? {"data-element-tags" => [:formatter].call(element.tag_list)} end |
#render_element(element, options = {}, counter = 1) ⇒ Object
If the view partial is not found alchemy/elements/_view_not_found.html.erb
gets rendered.
This helper renders a Alchemy::Element view partial.
A element view partial is the html snippet presented to the website visitor.
The partial is located in app/views/alchemy/elements
.
View partial naming
The partial has to be named after the name of the element as defined in the elements.yml
file.
Example
Given a headline element
# elements.yml
- name: headline
ingredients:
- role: text
type: Text
Then your element view partial has to be named like:
app/views/alchemy/elements/_headline.html.{erb|haml|slim}
Element partials generator
You can use this handy generator to let Alchemy generate the partials for you:
$ rails generate alchemy:elements --skip
Usage
<%= render_element(Alchemy::Element.published.named(:headline).first) %>
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/helpers/alchemy/elements_helper.rb', line 143 def render_element(element, = {}, counter = 1) if element.nil? warning("Element is nil") render "alchemy/elements/view_not_found", {name: "nil"} return end element.store_page(@page) render( partial: [:partial] || element.to_partial_path, object: element, locals: { element: element, counter: counter, options: .except(:locals, :partial) }.merge([:locals] || {}) ) rescue ActionView::MissingTemplate => e warning(%( Element view partial not found for #{element.name}.\n #{e} )) render "alchemy/elements/view_not_found", name: element.name end |
#render_elements(options = {}, &blk) ⇒ Object
Renders elements from given page
Examples:
Render only certain elements:
<header>
<%= render_elements only: ['header', 'claim'] %>
</header>
<section id="content">
<%= render_elements except: ['header', 'claim'] %>
</section>
Render elements from global page:
<footer>
<%= render_elements from_page: Alchemy::Page.find_by(page_layout: 'footer') %>
</footer>
Custom elements finder:
Having a custom element finder class:
class MyCustomNewsArchive
def elements(page:)
news_page.elements.named('news').order(created_at: :desc)
end
private
def news_page
Alchemy::Page.where(page_layout: 'news-archive')
end
end
In your view:
<div class="news-archive">
<%= render_elements finder: MyCustomNewsArchive.new %>
</div>
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/helpers/alchemy/elements_helper.rb', line 73 def render_elements( = {}, &blk) = { from_page: Current.page || @page, render_format: "html" }.update() finder = [:finder] || Alchemy::ElementsFinder.new() page_version = if Current.preview_page? [:from_page]&.draft_version else [:from_page]&.public_version end elements = finder.elements(page_version: page_version) default_rendering = ->(element, i) { render_element(element, , i + 1) } capture do if blk elements.map.with_index(&blk) else elements.map.with_index(&default_rendering) end.join([:separator]).html_safe end end |