Class: Usmu::Template::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/usmu/template/helpers.rb

Overview

Helper functions that get imported into the local scope of templates

Instance Method Summary collapse

Constructor Details

#initialize(configuration, layout) ⇒ Helpers

Create a new Helpers instance. These are created on demand as needed by templates, there is not a singleton instance.



10
11
12
13
# File 'lib/usmu/template/helpers.rb', line 10

def initialize(configuration, layout)
  @configuration = configuration
  @layout = layout
end

Instance Method Details

#collection(name) ⇒ void



52
53
54
# File 'lib/usmu/template/helpers.rb', line 52

def collection(name)
  @configuration.generator.collections[name]
end

#include(name, args = {}) ⇒ String

Finds and renders a named include.

Parameters:

  • name (String)

    The name of the include file. Should not include file extension.

  • args (Hash) (defaults to: {})

    The named arguments to provide to the include file. These are incorporated as additional metadata available to the include file.

Returns:

  • (String)

    The rendered file.



25
26
27
28
29
# File 'lib/usmu/template/helpers.rb', line 25

def include(name, args = {})
  inc = Usmu::Template::Include.find_include(@configuration, name)
  inc.arguments = args
  inc.render
end

#next_pagevoid



47
48
49
50
# File 'lib/usmu/template/helpers.rb', line 47

def next_page
  collection = @layout['page', 'collection', default: @layout['collection', default: '']]
  @configuration.generator.collections[collection].next_from(@layout['page', default: @layout])
end

#previous_pagevoid



42
43
44
45
# File 'lib/usmu/template/helpers.rb', line 42

def previous_page
  collection = @layout['page', 'collection', default: @layout['collection', default: '']]
  @configuration.generator.collections[collection].previous_from(@layout)
end

#url(path) ⇒ void



31
32
33
34
35
36
37
38
39
40
# File 'lib/usmu/template/helpers.rb', line 31

def url(path)
  path = if path.is_a? String
           path
         elsif path.respond_to? :output_filename
           path.output_filename
         else
           path.to_s
         end
  @configuration['base path', default: '/'] + path
end