Class: Solargraph::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/page.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory = VIEWS_PATH) ⇒ Page


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/solargraph/page.rb', line 40

def initialize directory = VIEWS_PATH
  directory = VIEWS_PATH if directory.nil? or !File.directory?(directory)
  directories = [directory]
  directories.push VIEWS_PATH if directory != VIEWS_PATH
  @render_method = proc { |template, layout: false, locals: {}|
    binder = Binder.new(locals, @render_method)
    if layout
      Tilt::ERBTemplate.new(Page.select_template(directories, 'layout')).render(binder) do
        Tilt::ERBTemplate.new(Page.select_template(directories, template)).render(binder)
      end
    else
      Tilt::ERBTemplate.new(Page.select_template(directories, template)).render(binder)
    end
  }
end

Class Method Details

.select_template(directories, name) ⇒ Object

Raises:


60
61
62
63
64
65
66
# File 'lib/solargraph/page.rb', line 60

def self.select_template directories, name
  directories.each do |dir|
    path = File.join(dir, "#{name}.erb")
    return path if File.file?(path)
  end
  raise FileNotFoundError, "Template not found: #{name}"
end

Instance Method Details

#render(template, layout: true, locals: {}) ⇒ Object


56
57
58
# File 'lib/solargraph/page.rb', line 56

def render template, layout: true, locals: {}
  @render_method.call(template, layout: layout, locals: locals)
end