Module: Alchemy::PagesHelper
- Includes:
- BaseHelper, ElementsHelper
- Defined in:
- app/helpers/alchemy/pages_helper.rb
Instance Method Summary collapse
-
#cell_empty?(name) ⇒ Boolean
Returns true or false if no elements are in the cell found by name.
-
#external_page_css_class(page) ⇒ Object
Returns ‘active’ if the given external page is in the current url path or
nil
. -
#language_links(options = {}) ⇒ Object
Renders links to language root pages of all published languages.
- #meta_description ⇒ Object
- #meta_keywords ⇒ Object
- #meta_robots ⇒ Object
-
#page_active?(page) ⇒ Boolean
Returns true if page is in the active branch.
-
#page_title(options = {}) ⇒ Object
Returns current page title.
- #picture_essence_caption(content) ⇒ Object
-
#render_breadcrumb(options = {}) ⇒ Object
Returns page links in a breadcrumb beginning from root to current page.
-
#render_cell(name, options = {}) ⇒ Object
Renders the partial for the cell with the given name of the current page.
-
#render_navigation(options = {}, html_options = {}) ⇒ Object
Renders the navigation.
-
#render_page_layout ⇒ Object
Renders the layout for current page.
-
#render_site_layout ⇒ Object
Renders a partial for current site.
-
#render_subnavigation(options = {}, html_options = {}) ⇒ Object
Renders navigation the children and all siblings of the given page (standard is the current page).
Methods included from ElementsHelper
#element_dom_id, #element_preview_code, #element_preview_code_attributes, #element_tags, #element_tags_attributes, #render_element, #render_elements, #sort_elements_by_content
Methods included from ElementsBlockHelper
#element_editor_for, #element_view_for
Methods included from UrlHelper
#download_alchemy_attachment_path, #download_alchemy_attachment_url, #full_url_for_element, #show_alchemy_page_path, #show_alchemy_page_url, #show_page_path_params
Methods included from EssencesHelper
#render_essence, #render_essence_view, #render_essence_view_by_name
Methods included from BaseHelper
#message_icon_class, #page_or_find, #render_flash_notice, #render_icon, #render_message, #shorten, #warning
Instance Method Details
#cell_empty?(name) ⇒ Boolean
Returns true or false if no elements are in the cell found by name.
317 318 319 320 321 |
# File 'app/helpers/alchemy/pages_helper.rb', line 317 def cell_empty?(name) cell = @page.cells.find_by_name(name) return true if cell.blank? cell.elements.not_trashed.empty? end |
#external_page_css_class(page) ⇒ Object
Returns ‘active’ if the given external page is in the current url path or nil
.
217 218 219 220 |
# File 'app/helpers/alchemy/pages_helper.rb', line 217 def external_page_css_class(page) return nil if !page.redirects_to_external? request.path.split('/').delete_if(&:blank?).first == page.urlname.gsub(/^\//, '') ? 'active' : nil end |
#language_links(options = {}) ⇒ Object
Renders links to language root pages of all published languages.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/helpers/alchemy/pages_helper.rb', line 26 def language_links( = {}) = { linkname: 'name', show_title: true, spacer: '', reverse: false }.merge() languages = Language.on_current_site.published.with_root_page.order("name #{[:reverse] ? 'DESC' : 'ASC'}") return nil if languages.count < 2 render( partial: "alchemy/language_links/language", collection: languages, spacer_template: "alchemy/language_links/spacer", locals: {languages: languages, options: } ) end |
#meta_description ⇒ Object
285 286 287 |
# File 'app/helpers/alchemy/pages_helper.rb', line 285 def @page..presence || Language.current_root_page.try(:meta_description) end |
#meta_keywords ⇒ Object
289 290 291 |
# File 'app/helpers/alchemy/pages_helper.rb', line 289 def @page..presence || Language.current_root_page.try(:meta_keywords) end |
#meta_robots ⇒ Object
293 294 295 |
# File 'app/helpers/alchemy/pages_helper.rb', line 293 def "#{@page.robot_index? ? '' : 'no'}index, #{@page.robot_follow? ? '' : 'no'}follow" end |
#page_active?(page) ⇒ Boolean
Returns true if page is in the active branch
211 212 213 214 |
# File 'app/helpers/alchemy/pages_helper.rb', line 211 def page_active?(page) @_page_ancestors ||= Page.ancestors_for(@page) @_page_ancestors.include?(page) end |
#page_title(options = {}) ⇒ Object
Returns current page title
Options:
prefix: "" # Prefix
separator: "" # Separating prefix and title
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'app/helpers/alchemy/pages_helper.rb', line 268 def page_title( = {}) return "" if @page.title.blank? = { prefix: "", suffix: "", separator: "" }.update() title_parts = [[:prefix]] if response.status == 200 title_parts << @page.title else title_parts << response.status end title_parts << [:suffix] title_parts.reject(&:blank?).join([:separator]).html_safe end |
#picture_essence_caption(content) ⇒ Object
8 9 10 |
# File 'app/helpers/alchemy/pages_helper.rb', line 8 def (content) content.try(:essence).try(:caption) end |
#render_breadcrumb(options = {}) ⇒ Object
Returns page links in a breadcrumb beginning from root to current page.
Options:
separator: %(<span class="separator">></span>) # Maybe you don't want this separator. Pass another one.
page: @page # Pass a different Page instead of the default (@page).
without: nil # Pass Page object or array of Pages that must not be displayed.
restricted_only: false # Pass boolean for displaying restricted pages only.
reverse: false # Pass boolean for displaying breadcrumb in reversed reversed.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'app/helpers/alchemy/pages_helper.rb', line 232 def ( = {}) = { separator: ">", page: @page, restricted_only: false, reverse: false, link_active_page: false }.merge() pages = Page. ancestors_for([:page]). accessible_by(current_ability, :see) if .delete(:restricted_only) pages = pages.restricted end if .delete(:reverse) pages = pages.reorder('lft DESC') end if [:without].present? without = .delete(:without) pages = pages.where.not(id: without.try(:collect, &:id) || without.id) end render 'alchemy/breadcrumb/wrapper', pages: pages, options: end |
#render_cell(name, options = {}) ⇒ Object
Renders the partial for the cell with the given name of the current page. Cell partials are located in app/views/cells/
of your project.
Options are:
from_page: Alchemy::Page # Alchemy::Page object from which the elements are rendered from.
locals: Hash # Hash of variables that will be available in the partial. Example: {user: var1, product: var2}
305 306 307 308 309 310 311 312 313 314 |
# File 'app/helpers/alchemy/pages_helper.rb', line 305 def render_cell(name, = {}) = { from_page: @page, locals: {} } = .merge() cell = [:from_page].cells.find_by_name(name) return "" if cell.blank? render partial: "alchemy/cells/#{name}", locals: {cell: cell}.merge([:locals]) end |
#render_navigation(options = {}, html_options = {}) ⇒ Object
Renders the navigation.
It produces a html <ul><li></li></ul> structure with all necessary classes so you can produce every navigation the web uses today. I.E. dropdown-navigations, simple mainnavigations or even complex nested ones.
HTML output:
<ul class="navigation level_1">
<li class="first home"><a href="/home" class="active" title="Homepage" lang="en" data-page-id="1">Homepage</a></li>
<li class="contact"><a href="/contact" title="Contact" lang="en" data-page-id="2">Contact</a></li>
<li class="last imprint"><a href="/imprint" title="Imprint" lang="en" data-page-id="3">Imprint</a></li>
</ul>
As you can see: Everything you need.
Not pleased with the way Alchemy produces the navigation structure?
Then feel free to overwrite the partials (_renderer.html.erb and _link.html.erb) found in views/navigation/
or pass different partials via the options :navigation_partial
and :navigation_link_partial
.
Passing HTML classes and ids to the renderer
A second hash can be passed as html_options to the navigation renderer partial.
Example:
<%= render_navigation({from_page: 'subnavi'}, {class: 'navigation', id: 'subnavigation'}) %>
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/helpers/alchemy/pages_helper.rb', line 151 def ( = {}, = {}) = { submenu: false, all_sub_menues: false, from_page: @root_page || Language.current_root_page, spacer: nil, navigation_partial: 'alchemy/navigation/renderer', navigation_link_partial: 'alchemy/navigation/link', show_nonactive: false, restricted_only: false, show_title: true, reverse: false, reverse_children: false }.merge() page = page_or_find([:from_page]) return nil if page.blank? pages = page.children.accessible_by(current_ability, :see) pages = pages.restricted if .delete(:restricted_only) if depth = [:deepness] pages = pages.where('depth <= ?', depth) end if [:reverse] pages.reverse! end render [:navigation_partial], options: , pages: pages, html_options: end |
#render_page_layout ⇒ Object
Renders the layout for current page.
Page layout files belongs in /app/views/alchemy/page_layouts/
Falls back to /app/views/alchemy/page_layouts/standard
if the page_layout partial is not found.
49 50 51 52 53 54 |
# File 'app/helpers/alchemy/pages_helper.rb', line 49 def render_page_layout render @page, page: @page rescue ActionView::MissingTemplate warning("PageLayout: '#{@page.page_layout}' not found. Rendering standard page_layout.") render 'alchemy/page_layouts/standard', page: @page end |
#render_site_layout ⇒ Object
Renders a partial for current site
Place a rails partial into app/views/alchemy/site_layouts
and name it like your site name.
Example:
<%= render_site_layout %>
renders app/views/alchemy/site_layouts/_default_site.html.erb
for the site named “Default Site”.
68 69 70 71 72 73 |
# File 'app/helpers/alchemy/pages_helper.rb', line 68 def render_site_layout render current_alchemy_site rescue ActionView::MissingTemplate warning("Site layout for #{current_alchemy_site.try(:name)} not found. Please run `rails g alchemy:site_layouts`") "" end |
#render_subnavigation(options = {}, html_options = {}) ⇒ Object
Renders navigation the children and all siblings of the given page (standard is the current page).
Use this helper if you want to render the subnavigation independent from the mainnavigation. I.E. to place it in a different area on your website.
This helper passes all its options to the the render_navigation helper.
Options:
from_page: @page # The page to render the navigation from
submenu: true # Shows the nested children
level: 2 # Normally there is no need to change the level parameter, just in a few special cases
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'app/helpers/alchemy/pages_helper.rb', line 193 def ( = {}, = {}) = { from_page: @page, submenu: true, level: 2 } = .merge() if ![:from_page].nil? while [:from_page].level > [:level] [:from_page] = [:from_page].parent end (, ) else return nil end end |