Module: Proton::Helpers

Defined in:
lib/proton/helpers.rb

Instance Method Summary collapse

Instance Method Details

#content_blocksObject



92
93
94
95
# File 'lib/proton/helpers.rb', line 92

def content_blocks
  $content_blocks ||= Hash.new
  $content_blocks[page.path] ||= Hash.new
end

#content_for(key, &blk) ⇒ Object

Helper: content_for (Helpers) Content for.

See also

  • Helpers:has_content?
  • Helpers:content_for
  • Helpers:yield_content


88
89
90
# File 'lib/proton/helpers.rb', line 88

def content_for(key, &blk)
  content_blocks[key.to_sym] = blk
end

#has_content?(key) ⇒ Boolean

Helper: has_content? (Helpers) Checks if there's something defined for a given content block.

Example

See Helpers:content_for for an example.

See also

  • Helpers:has_content?
  • Helpers:content_for
  • Helpers:yield_content

Returns:



108
109
110
# File 'lib/proton/helpers.rb', line 108

def has_content?(key)
  content_blocks.member? key.to_sym
end

#partial(path, locals = {}) ⇒ Object

Helper: partial (Helpers) Renders a partial.

Usage

<%= partial path, locals %>

Description

See Introduction: Partials for more info.

Example

If your _layouts/_banner.erb looks like this:

<div class='banner'>
  Welcome to <%= start.title %>
</div>

...Then this will embed the partial _layouts/_nav.erb. The partial will be rendered with start being set to the current page.

<%= partial '_banner', :start => page %>


36
37
38
39
# File 'lib/proton/helpers.rb', line 36

def partial(path, locals={})
  partial = Partial[path.to_s, page]  or return ''
  partial.to_html locals.merge(:page => self)
end

#rel(path, from = page.path) ⇒ Object

Helper: rel (Helpers) Turns a path into a relative path.

Usage

<%= rel(path) %>

Description

rel takes a given absolute path that begins with a / (for instance, /x/y/z.html) and returns a relative path (maybe ../y/z.html). This is useful if your site will not be be hosted on it's own domain.

Example

<% page.children.each do |child| %>
  <a href="<%= rel(child.path) %>">
    <%= child.title %>
  </a>
<% end %>

This may output:

<a href="../../foo.html">
  Foobar
</a>

...where the ../../ depends on the current page's path.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/proton/helpers.rb', line 67

def rel(path, from=page.path)
  if path[0] == '/'
    depth = from.count('/')
    dotdot = depth > 1 ? ('../' * (depth-1)) : './'
    str = (dotdot[0...-1] + path).squeeze('/')
    str = str[2..-1]  if str[0..-1] == './'
    str
  else
    path
  end
end

#yield_content(key, *args) ⇒ Object

Helper: yield_content (Helpers) Yield

Example

See Helpers:content_for for an example.

See also

  • Helpers:has_content?
  • Helpers:content_for
  • Helpers:yield_content


123
124
125
126
127
128
129
130
# File 'lib/proton/helpers.rb', line 123

def yield_content(key, *args)
  content = content_blocks[key.to_sym]
  if respond_to?(:block_is_haml?) && block_is_haml?(content)
    capture_haml(*args, &content)
  elsif content
    content.call(*args)
  end
end