Class: Bridgetown::Layout
- Inherits:
-
Object
- Object
- Bridgetown::Layout
- Includes:
- FrontMatter::Importer
- Defined in:
- lib/bridgetown-core/layout.rb
Instance Attribute Summary collapse
-
#content ⇒ String
Gets/Sets the content of this layout.
-
#current_document ⇒ Object
Gets/Sets the current document (for layout-compatible converters).
-
#current_document_output ⇒ Object
Gets/Sets the document output (for layout-compatible converters).
-
#data ⇒ Object
Gets/Sets the Hash that holds the metadata for this layout.
-
#ext ⇒ Object
(also: #extname)
Gets/Sets the extension of this layout.
- #front_matter_line_count ⇒ Integer
-
#name ⇒ Object
readonly
Gets the name of this layout.
-
#path ⇒ Object
readonly
Gets the path to this layout.
-
#relative_path ⇒ Object
readonly
Gets the path to this layout relative to its base.
-
#site ⇒ Object
readonly
Gets the Site object.
Class Method Summary collapse
-
.label_for_file(file) ⇒ String
Determines the label a layout should use based on its filename.
Instance Method Summary collapse
-
#[](property) ⇒ String?
Accessor for data properties by Liquid.
- #handle_read_error(error) ⇒ Object
-
#initialize(site, base, name, from_plugin: false) ⇒ Layout
constructor
Initialize a new Layout.
-
#inspect ⇒ String
The inspect string for this layout.
-
#label ⇒ String
The label of the layout (should match what would used in front matter references).
-
#to_liquid ⇒ HashWithDotAccess::Hash
Provide this Layout's data for use by Liquid.
-
#to_s ⇒ Object
Returns the contents as a String.
Methods included from FrontMatter::Importer
Constructor Details
#initialize(site, base, name, from_plugin: false) ⇒ Layout
Initialize a new Layout.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/bridgetown-core/layout.rb', line 55 def initialize(site, base, name, from_plugin: false) @site = site @base = base @name = name if from_plugin @base_dir = base.sub("/layouts", "") @path = File.join(base, name) else @base_dir = site.source @path = site.in_source_dir(base, name) end @relative_path = @path.sub(@base_dir, "") @ext = File.extname(name) @data = read_front_matter(@path)&.as_dots rescue SyntaxError => e Bridgetown.logger.error "Error:", "Ruby Exception in #{e.}" rescue StandardError => e handle_read_error(e) ensure @data ||= HashWithDotAccess::Hash.new end |
Instance Attribute Details
#content ⇒ String
Gets/Sets the content of this layout.
30 31 32 |
# File 'lib/bridgetown-core/layout.rb', line 30 def content @content end |
#current_document ⇒ Object
Gets/Sets the current document (for layout-compatible converters)
36 37 38 |
# File 'lib/bridgetown-core/layout.rb', line 36 def current_document @current_document end |
#current_document_output ⇒ Object
Gets/Sets the document output (for layout-compatible converters)
39 40 41 |
# File 'lib/bridgetown-core/layout.rb', line 39 def current_document_output @current_document_output end |
#data ⇒ Object
Gets/Sets the Hash that holds the metadata for this layout.
26 27 28 |
# File 'lib/bridgetown-core/layout.rb', line 26 def data @data end |
#ext ⇒ Object Also known as: extname
Gets/Sets the extension of this layout.
21 22 23 |
# File 'lib/bridgetown-core/layout.rb', line 21 def ext @ext end |
#front_matter_line_count ⇒ Integer
33 34 35 |
# File 'lib/bridgetown-core/layout.rb', line 33 def front_matter_line_count @front_matter_line_count end |
#name ⇒ Object (readonly)
Gets the name of this layout.
12 13 14 |
# File 'lib/bridgetown-core/layout.rb', line 12 def name @name end |
#path ⇒ Object (readonly)
Gets the path to this layout.
15 16 17 |
# File 'lib/bridgetown-core/layout.rb', line 15 def path @path end |
#relative_path ⇒ Object (readonly)
Gets the path to this layout relative to its base
18 19 20 |
# File 'lib/bridgetown-core/layout.rb', line 18 def relative_path @relative_path end |
#site ⇒ Object (readonly)
Gets the Site object.
9 10 11 |
# File 'lib/bridgetown-core/layout.rb', line 9 def site @site end |
Class Method Details
.label_for_file(file) ⇒ String
Determines the label a layout should use based on its filename
45 46 47 |
# File 'lib/bridgetown-core/layout.rb', line 45 def self.label_for_file(file) file.split(".").first end |
Instance Method Details
#[](property) ⇒ String?
Accessor for data properties by Liquid
102 103 104 |
# File 'lib/bridgetown-core/layout.rb', line 102 def [](property) data[property] end |
#handle_read_error(error) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bridgetown-core/layout.rb', line 80 def handle_read_error(error) if error.is_a? Psych::SyntaxError Bridgetown.logger.warn "YAML Exception reading #{@path}: #{error.}" else Bridgetown.logger.warn "Error reading file #{@path}: #{error.}" end if site.config["strict_front_matter"] || error.is_a?(Bridgetown::Errors::FatalException) raise error end end |
#inspect ⇒ String
The inspect string for this layout. Includes the relative path.
117 118 119 |
# File 'lib/bridgetown-core/layout.rb', line 117 def inspect "#<#{self.class} #{relative_path}>" end |
#label ⇒ String
The label of the layout (should match what would used in front matter references).
110 111 112 |
# File 'lib/bridgetown-core/layout.rb', line 110 def label @label ||= self.class.label_for_file(name) end |
#to_liquid ⇒ HashWithDotAccess::Hash
Provide this Layout's data for use by Liquid.
124 125 126 |
# File 'lib/bridgetown-core/layout.rb', line 124 def to_liquid data end |
#to_s ⇒ Object
Returns the contents as a String.
94 95 96 |
# File 'lib/bridgetown-core/layout.rb', line 94 def to_s content || "" end |