Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add layout method to Resource::Base #324

Merged
merged 1 commit into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bridgetown-core/features/hooks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Hooks
def initialize(site, base)
@site = site
@base = base
@data = {}
@data = HashWithDotAccess::Hash.new
@dir = '/'
@name = 'foo.html'
@content = 'mytinypage'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def place_in_layout?
end

def no_layout?
data["layout"] == "none" || data["layout"] == false
data.layout.nil? || data.layout == "none" || data.layout == false
end
end
end
37 changes: 27 additions & 10 deletions bridgetown-core/lib/bridgetown-core/layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@ class Layout
# Gets/Sets the document output (for layout-compatible converters)
attr_accessor :current_document_output

# Determines the label a layout should use based on its filename
#
# @param file [String]
# @return [String]
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

# Initialize a new Layout.
#
# site - The Site.
# base - The String path to the source.
# name - The String filename of the layout file.
# from_plugin - true if the layout comes from a Gem-based plugin folder.
# @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.
def initialize(site, base, name, from_plugin: false)
@site = site
@base = base
Expand Down Expand Up @@ -83,17 +93,24 @@ def handle_read_error(error)
end
end

# The inspect string for this document.
# Includes the relative path and the collection label.
# The label of the layout (should match what would used in front matter
# references).
#
# Returns the inspect string for this document.
# @return [String]
def label
@label ||= self.class.label_for_file(name)
end

# The inspect string for this layout. Includes the relative path.
#
# @return [String]
def inspect
"#<#{self.class} #{@path}>"
"#<#{self.class} #{relative_path}>"
end

# Provide this Layout's data to a Hash suitable for use by Liquid.
# Provide this Layout's data for use by Liquid.
#
# Returns the Hash representation of this Layout.
# @return [HashWithDotAccess::Hash]
def to_liquid
data
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def entries_in(dir)
end

def layout_name(file)
file.split(".")[0..-2].join(".")
Layout.label_for_file(file)
end

def within(directory)
Expand Down
16 changes: 16 additions & 0 deletions bridgetown-core/lib/bridgetown-core/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ def collection
model.collection
end

# Layout associated with this resource
# This will output a warning if the layout can't be found.
#
# @return [Bridgetown::Layout]
def layout
return @layout if @layout
return if no_layout?

@layout = site.layouts[data.layout].tap do |layout|
unless layout
Bridgetown.logger.warn "Resource:", "Layout '#{data.layout}' " \
"requested via #{relative_path} does not exist."
end
end
end

# The relative path of source file or file-like origin
#
# @return [Pathname]
Expand Down
9 changes: 9 additions & 0 deletions bridgetown-core/test/resources/src/_layouts/default.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
---
<!doctype html>
<html>
<head></head>
<body>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions bridgetown-core/test/resources/src/top-level-page.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
layout: default
title: I'm a Top Level Page!
---

Expand Down
4 changes: 4 additions & 0 deletions bridgetown-core/test/test_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class TestResource < BridgetownUnitTest
assert_equal "pages", @resource.collection.label
end

should "know its layout" do
assert_equal "default", @resource.layout.label
end

should "know whether it's a YAML file" do
assert_equal false, @resource.yaml_file?
end
Expand Down