Class: Roger::Template

Inherits:
BlankTemplate show all
Defined in:
lib/roger/template.rb,
lib/roger/template/helpers/capture.rb,
lib/roger/template/helpers/partial.rb,
lib/roger/template/template_context.rb,
lib/roger/template/helpers/rendering.rb

Overview

Roger template processing class

Defined Under Namespace

Modules: Helpers Classes: TemplateContext

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, context = nil, options = {}) ⇒ Template

Returns a new instance of Template.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :source_path (String, Pathname)

    The path to the source of the template being processed



41
42
43
44
45
46
47
48
# File 'lib/roger/template.rb', line 41

def initialize(source, context = nil, options = {})
  @context = context

  self.source_path = options[:source_path]
  self.data, self.source = extract_front_matter(source)

  @templates = Tilt.templates_for(source_path)
end

Instance Attribute Details

#current_tilt_templateObject (readonly)

The current tilt template being used



31
32
33
# File 'lib/roger/template.rb', line 31

def current_tilt_template
  @current_tilt_template
end

#dataObject

Store the frontmatter



25
26
27
# File 'lib/roger/template.rb', line 25

def data
  @data
end

#sourceObject

The source



22
23
24
# File 'lib/roger/template.rb', line 22

def source
  @source
end

#source_pathObject

The path to the source file for this template



28
29
30
# File 'lib/roger/template.rb', line 28

def source_path
  @source_path
end

Class Method Details

.open(path, context = nil, options = {}) ⇒ Object



34
35
36
# File 'lib/roger/template.rb', line 34

def open(path, context = nil, options = {})
  new(File.read(path), context, options.update(source_path: path))
end

Instance Method Details

#real_source_pathObject

Actual path on disk, nil if it doesn’t exist The nil case is mostly used with inline rendering.



58
59
60
61
62
63
64
65
# File 'lib/roger/template.rb', line 58

def real_source_path
  return @_real_source_path if @_real_source_path_cached

  @_real_source_path_cached = true
  @_real_source_path = if File.exist?(source_path)
                         Pathname.new(source_path).realpath
                       end
end

#render(locals = {}, &block) ⇒ Object



50
51
52
53
54
# File 'lib/roger/template.rb', line 50

def render(locals = {}, &block)
  @templates.inject(source) do |src, template|
    render_with_tilt_template(template, src, locals, &block)
  end
end