module Alchemy
module PagesHelper
include Alchemy::ElementsHelper
def render_classes(classes=[])
s = classes.uniq.delete_if { |x| x.blank? }.join(" ")
s.blank? ? "" : "class='#{s}'"
end
def picture_essence_caption(content)
return "" if content.nil?
return "" if content.essence.nil?
content.essence.caption
end
# == Helper for rendering language switches
#
# Renders links to all public language root pages
#
# === Options:
#
# :linkname => :name,
# :spacer => "",
# :link_to_public_child => configuration(:redirect_to_public_child),
# :link_to_page_with_layout => nil,
# :show_title => true,
# :reverse => false,
# :as_select_box => false,
# :show_flags => false
#
def language_switcher(options={})
default_options = {
:linkname => :name,
:spacer => "",
:link_to_public_child => configuration(:redirect_to_public_child),
:link_to_page_with_layout => nil,
:show_title => true,
:reverse => false,
:as_select_box => false,
:show_flags => false
}
options = default_options.merge(options)
if multi_language?
language_links = []
pages = (options[:link_to_public_child] == true) ? Page.language_roots : Page.public_language_roots
return nil if (pages.blank? || pages.length == 1)
pages.each_with_index do |page, i|
if(options[:link_to_page_with_layout] != nil)
page_found_by_layout = Page.where(:page_layout => options[:link_to_page_with_layout].to_s, :language_id => page.language_id)
end
page = page_found_by_layout || page
page = (options[:link_to_public_child] ? (page.first_public_child.blank? ? nil : page.first_public_child) : nil) if !page.public?
if !page.blank?
active = session[:language_id] == page.language.id
linkname = page.language.label(options[:linkname])
if options[:as_select_box]
language_links << [linkname, show_page_url(:urlname => page.urlname, :lang => page.language.code)]
else
language_links << link_to(
"#{content_tag(:span, '', :class => "flag") if options[:show_flags]}#{ content_tag(:span, linkname)}".html_safe,
alchemy.show_page_path(:urlname => page.urlname, :lang => page.language.code),
:class => "#{(active ? 'active ' : nil)}#{page.language.code} #{(i == 0) ? 'first' : (i==pages.length-1) ? 'last' : nil}",
:title => options[:show_title] ? Alchemy::I18n.t("alchemy.language_links.#{page.language.code}.title", :default => page.language.name) : nil
)
end
end
# when last iteration and we have just one language_link,
# we dont need to render it.
if (i==pages.length-1) && language_links.length == 1
return nil
end
end
return nil if language_links.empty? || language_links.length == 1
language_links.reverse! if options[:reverse]
if options[:as_select_box]
return select_tag(
'language',
options_for_select(
language_links,
show_page_url(:urlname => @page.urlname, :lang => @page.language.code)
),
:onchange => "window.location=this.value"
)
else
raw(language_links.join(options[:spacer]))
end
else
nil
end
end
alias_method :language_switches, :language_switcher
# Renders the layout from @page.page_layout. File resists in /app/views/page_layouts/_LAYOUT-NAME.html.erb
def render_page_layout(options={})
default_options = {
:render_format => "html"
}
options = default_options.merge(options)
render :partial => "alchemy/page_layouts/#{@page.page_layout.downcase}.#{options[:render_format]}.erb"
rescue ActionView::MissingTemplate
warning("PageLayout: '#{@page.page_layout}' not found. Rendering standard page_layout.")
render :partial => "alchemy/page_layouts/standard"
end
def sitename_from_header_page
header_page = Page.find_by_page_layout_and_layoutpage('layout_header', true)
return "" if header_page.nil?
page_title = header_page.elements.find_by_name('sitename')
return "" if page_title.nil?
page_title.ingredient('name')
end
# Renders the navigation.
#
# It produces a html
structure with all necessary classes and ids so you can produce every navigation the web uses today.
# I.E. dropdown-navigations, simple mainnavigations or even complex nested ones.
#
# === En detail:
#
#
#
# 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+.
#
# === The options are:
#
# :submenu => false # Do you want a nested
structure for the deeper levels of your navigation, or not? Used to display the subnavigation within the mainnaviagtion. E.g. for dropdown menues.
# :all_sub_menues => false # Renders the whole page tree.
# :from_page => @root_page # Do you want to render a navigation from a different page then the current page? Then pass an Page instance or a Alchemy::PageLayout name as string.
# :spacer => "" # Yeah even a spacer for the entries can be passed. Simple string, or even a complex html structure. E.g: "|". Only your imagination is the limit. And the W3C of course :)
# :navigation_partial => "navigation/renderer" # Pass a different partial to be taken for the navigation rendering. CAUTION: Only for the advanced Alchemy webdevelopers. The standard partial takes care of nearly everything. But maybe you are an adventures one ^_^
# :navigation_link_partial => "navigation/link" # Alchemy places an html link in