Module: SmartTitles::Helper

Defined in:
lib/smart_titles/helper.rb

Instance Method Summary collapse

Instance Method Details

#head_title(default_title = t('.title', default: :title)) ⇒ Object

<title><%= head_title %></title> Will return title if it was set for the current page. Otherwise will return :title translation.

<title><%= head_title “My Blog” %></title> The default title will be “My Blog” instead of :title translation.

Example usage with title template:

en:
  title: The Coolest Store
  title_template: %{title} from the Coolest Store

head_title     # => The Coolest Store
title "Drinks" # => <h1>Drinks</h1>
head_title     # => Drinks from the Coolest Store


18
19
20
21
22
23
24
25
# File 'lib/smart_titles/helper.rb', line 18

def head_title(default_title = t('.title', default: :title))
  if content_for?(:page_title)
    title = content_for(:page_title)
    t('.title_template', title: title, default: [:title_template, title])
  else
    default_title
  end
end

#title(custom_title_or_options = nil) ⇒ Object

Convinient helper method that will:

  • Set custom title for the current page if it is passed. Otherwise the title will be automatically set

  • Return the title passed or looked up from locale wrapped into h1 tag



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/smart_titles/helper.rb', line 30

def title(custom_title_or_options = nil)
  case custom_title_or_options
  when Hash
    options = custom_title_or_options
  else
    options = {}
    custom_title = custom_title_or_options
  end

  title = custom_title
  catch(:exception) do
    title ||= t('.title', options.merge(throw: true))
  end

  provide(:page_title, title && title.html_safe) # "provide" already escapes
  (:h1, title) if title
end