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

Returns a new instance of Page.

Parameters:

  • directory (String) (defaults to: VIEWS_PATH)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/solargraph/page.rb', line 44

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
  # @type [Proc]
  # @param template [String]
  # @param layout [Boolean]
  # @param locals [Hash]
  @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) ⇒ String

Parameters:

  • directories (Array<String>)
  • name (String)

Returns:

  • (String)

Raises:



75
76
77
78
79
80
81
# File 'lib/solargraph/page.rb', line 75

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: {}) ⇒ String

Parameters:

  • template (String)
  • layout (Boolean) (defaults to: true)
  • locals (Hash) (defaults to: {})

Returns:

  • (String)


68
69
70
# File 'lib/solargraph/page.rb', line 68

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