Module: Sinatra::Filler

Defined in:
lib/sinatra/filler.rb

Instance Method Summary collapse

Instance Method Details

#filler_for(key, &block) ⇒ Object

Capture a block of filler to be rendered later. For example:

<% filler_for :head do %>
  <script type="text/javascript" src="/foo.js"></script>
<% end %>

You can call filler_for multiple times with the same key (in the example :head), and when you render the blocks for that key all of them will be rendered, in the same order you captured them.

Your blocks can also receive values, which are passed to them by yield_filler



16
17
18
# File 'lib/sinatra/filler.rb', line 16

def filler_for(key, &block)
  filler_blocks[key.to_sym] << block
end

#filler_for?(key) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/sinatra/filler.rb', line 20

def filler_for?(key)
  filler_blocks.has_key?(key.to_sym)
end

#javascripts(*args) ⇒ Object



80
81
82
# File 'lib/sinatra/filler.rb', line 80

def javascripts(*args)
  filler_for(:head) { javascript_script_tag(*args) }
end

#output_filler_for(key) ⇒ Object

Render the captured blocks for a given key. For example:

<head>
  <title>Example</title>
  <% output_filler_for :head %>
</head>

Would render everything you declared with filler_for :head before closing the <head> tag.

You can also pass values to the filler blocks by passing them as arguments after the key:

<% yield_filler :head %>


38
39
40
41
42
# File 'lib/sinatra/filler.rb', line 38

def output_filler_for(key)
  filler_blocks[key.to_sym].map do |content|
    content.call
  end.join
end

#show_title?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/sinatra/filler.rb', line 62

def show_title?
  @show_title
end

#stylesheets(*args) ⇒ Object

Capture content in templates:

<% javascripts “/js/application.js” %> <% stylesheets “/css/application.css” %>

Use the captured blocks in layouts:

<head>

<%= output_filler_for(:head) %>

</head>



76
77
78
# File 'lib/sinatra/filler.rb', line 76

def stylesheets(*args)
  filler_for(:head) { stylesheet_link_tag(*args) }
end

#title(page_title, show_title = true) ⇒ Object

Capture title in templates:

<% title “Sinatra is awesome!” %>

Use the captured block in layouts:

<head>

<title><%= filler_for?(:title) ? output_filler_for(:title) : "Untitled" %></title>

</head> <body> <% if filler_for?(:title) && show_title? %>

<h1><%= output_filler_for(:title) %></h1>

<% end %>



57
58
59
60
# File 'lib/sinatra/filler.rb', line 57

def title(page_title, show_title = true)
  filler_for(:title) { page_title.to_s }
  @show_title = show_title
end