Class: Ottogen::Layout
- Inherits:
-
Object
- Object
- Ottogen::Layout
- Defined in:
- lib/ottogen/layout.rb
Defined Under Namespace
Constant Summary collapse
- LAYOUTS_DIR =
'_layouts'- EXTENSION =
'.html.erb'
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#front_matter ⇒ Object
readonly
Returns the value of attribute front_matter.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(name:, front_matter:, body:) ⇒ Layout
constructor
A new instance of Layout.
- #render(content:, site:, page:) ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
25 26 27 |
# File 'lib/ottogen/layout.rb', line 25 def body @body end |
#front_matter ⇒ Object (readonly)
Returns the value of attribute front_matter.
25 26 27 |
# File 'lib/ottogen/layout.rb', line 25 def front_matter @front_matter end |
#name ⇒ Object (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. 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 |