class Bridgetown::Layout

Attributes

content[RW]

Gets/Sets the content of this layout. @return [String]

current_document[RW]

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

current_document_output[RW]

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

data[RW]

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

ext[RW]

Gets/Sets the extension of this layout.

extname[RW]

Gets/Sets the extension of this layout.

front_matter_line_count[RW]

@return [Integer]

name[R]

Gets the name of this layout.

path[R]

Gets the path to this layout.

relative_path[R]

Gets the path to this layout relative to its base

site[R]

Gets the Site object.

Public Class Methods

label_for_file(file) click to toggle source

Determines the label a layout should use based on its filename

@param file [String] @return [String]

# File lib/bridgetown-core/layout.rb, line 46
def self.label_for_file(file)
  # TODO: refactor this so multi-extension layout filenames don't leak
  # middle extensions into layout label
  file.split(".")[0..-2].join(".")
end
new(site, base, name, from_plugin: false) click to toggle source

Initialize a new Layout.

@param site [Bridgetown::Site] @param base [String] The path to the source. @param name [String] The filename of the layout file. @param from_plugin [Boolean] if the layout comes from a Gem-based plugin folder.

# File lib/bridgetown-core/layout.rb, line 58
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)&.with_dot_access
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

Public Instance Methods

handle_read_error(error) click to toggle source
# File lib/bridgetown-core/layout.rb, line 83
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
inspect() click to toggle source

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

@return [String]

# File lib/bridgetown-core/layout.rb, line 107
def inspect
  "#<#{self.class} #{relative_path}>"
end
label() click to toggle source

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

@return [String]

# File lib/bridgetown-core/layout.rb, line 100
def label
  @label ||= self.class.label_for_file(name)
end
to_liquid() click to toggle source

Provide this Layout's data for use by Liquid.

@return [HashWithDotAccess::Hash]

# File lib/bridgetown-core/layout.rb, line 114
def to_liquid
  data
end