Class: Ottogen::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/ottogen/layout.rb

Defined Under Namespace

Classes: Context, Error

Constant Summary collapse

LAYOUTS_DIR =
'_layouts'
EXTENSION =
'.html.erb'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, front_matter:, body:) ⇒ Layout

Returns a new instance of Layout.



27
28
29
30
31
# File 'lib/ottogen/layout.rb', line 27

def initialize(name:, front_matter:, body:)
  @name = name
  @front_matter = front_matter
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



25
26
27
# File 'lib/ottogen/layout.rb', line 25

def body
  @body
end

#front_matterObject (readonly)

Returns the value of attribute front_matter.



25
26
27
# File 'lib/ottogen/layout.rb', line 25

def front_matter
  @front_matter
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/ottogen/layout.rb', line 25

def name
  @name
end

Class Method Details

.find(name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/ottogen/layout.rb', line 14

def self.find(name)
  path = File.join(LAYOUTS_DIR, "#{name}#{EXTENSION}")
  raise Error, "layout '#{name}' not found at #{path}" unless File.exist?(path)

  raw = File.read(path)
  front_matter, body = FrontMatter.split(raw, path)
  new(name: name, front_matter: front_matter, body: body)
rescue FrontMatter::Error => e
  raise Error, e.message
end

Instance Method Details

#render(content:, site:, page:) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ottogen/layout.rb', line 33

def render(content:, site:, page:)
  context = Context.new(content: content, site: site, page: page)
  result = ERB.new(@body, trim_mode: '-').result(context.binding_for_erb)
  parent_name = @front_matter['layout']
  return result unless parent_name

  Layout.find(parent_name).render(content: result, site: site, page: page)
end