Class: Bridgetown::Layout

Inherits:
Object
  • Object
show all
Includes:
FrontMatter::Importer
Defined in:
bridgetown-core/lib/bridgetown-core/layout.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FrontMatter::Importer

included, #read_front_matter

Constructor Details

#initialize(site, base, name, from_plugin: false) ⇒ Layout

Initialize a new Layout.

Parameters:

  • site (Bridgetown::Site)
  • base (String)

    The path to the source.

  • name (String)

    The filename of the layout file.

  • from_plugin (Boolean) (defaults to: false)

    if the layout comes from a Gem-based plugin folder.



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 'bridgetown-core/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.message}"
rescue StandardError => e
  handle_read_error(e)
ensure
  @data ||= HashWithDotAccess::Hash.new
end

Instance Attribute Details

#contentString

Gets/Sets the content of this layout.

Returns:

  • (String)


30
31
32
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 30

def content
  @content
end

#current_documentObject

Gets/Sets the current document (for layout-compatible converters)



36
37
38
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 36

def current_document
  @current_document
end

#current_document_outputObject

Gets/Sets the document output (for layout-compatible converters)



39
40
41
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 39

def current_document_output
  @current_document_output
end

#dataObject

Gets/Sets the Hash that holds the metadata for this layout.



26
27
28
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 26

def data
  @data
end

#extObject Also known as: extname

Gets/Sets the extension of this layout.



21
22
23
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 21

def ext
  @ext
end

#front_matter_line_countInteger

Returns:

  • (Integer)


33
34
35
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 33

def front_matter_line_count
  @front_matter_line_count
end

#nameObject (readonly)

Gets the name of this layout.



12
13
14
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 12

def name
  @name
end

#pathObject (readonly)

Gets the path to this layout.



15
16
17
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 15

def path
  @path
end

#relative_pathObject (readonly)

Gets the path to this layout relative to its base



18
19
20
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 18

def relative_path
  @relative_path
end

#siteObject (readonly)

Gets the Site object.



9
10
11
# File 'bridgetown-core/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

Parameters:

  • file (String)

Returns:

  • (String)


45
46
47
# File 'bridgetown-core/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

Parameters:

  • property (String)

    name of the property to retrieve

Returns:

  • (String, nil)

    value or nil if the property isn’t included



102
103
104
# File 'bridgetown-core/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 'bridgetown-core/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.message}"
  else
    Bridgetown.logger.warn "Error reading file #{@path}: #{error.message}"
  end

  if site.config["strict_front_matter"] ||
      error.is_a?(Bridgetown::Errors::FatalException)
    raise error
  end
end

#inspectString

The inspect string for this layout. Includes the relative path.

Returns:

  • (String)


117
118
119
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 117

def inspect
  "#<#{self.class} #{relative_path}>"
end

#labelString

The label of the layout (should match what would used in front matter references).

Returns:

  • (String)


110
111
112
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 110

def label
  @label ||= self.class.label_for_file(name)
end

#to_liquidHashWithDotAccess::Hash

Provide this Layout’s data for use by Liquid.



124
125
126
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 124

def to_liquid
  data
end

#to_sObject

Returns the contents as a String.



94
95
96
# File 'bridgetown-core/lib/bridgetown-core/layout.rb', line 94

def to_s
  content || ""
end