-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Viewable
callable object mixin for Ruby components (#959)
- Loading branch information
1 parent
4d3eea6
commit 16128e0
Showing
15 changed files
with
209 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bridgetown | ||
# This mixin for Bridgetown components allows you to provide front matter and render | ||
# the component template via the layouts transformation pipeline, which can be called | ||
# from any Roda route | ||
module Viewable | ||
include Bridgetown::RodaCallable | ||
include Bridgetown::Transformable | ||
|
||
def site | ||
@site ||= Bridgetown::Current.site | ||
end | ||
|
||
def data | ||
@data ||= HashWithDotAccess::Hash.new | ||
end | ||
|
||
def front_matter(&block) | ||
Bridgetown::FrontMatter::RubyFrontMatter.new(data:).tap { _1.instance_exec(&block) } | ||
end | ||
|
||
def relative_path = self.class.source_location.delete_prefix("#{site.root_dir}/") | ||
|
||
# Render the component template in the layout specified in your front matter | ||
# | ||
# @param app [Roda] | ||
def render_in_layout(app) | ||
render_in(app) => rendered_output | ||
|
||
site.validated_layouts_for(self, data.layout).each do |layout| | ||
transform_with_layout(layout, rendered_output, self) => rendered_output | ||
end | ||
|
||
rendered_output | ||
end | ||
|
||
# Pass a block of front matter and render the component template in layouts | ||
# | ||
# @param app [Roda] | ||
def render_with(app, &) | ||
front_matter(&) | ||
render_in_layout(app) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h2><%= @title %></h2> | ||
|
||
<%= markdownify do %> | ||
* Port <%= @port_number %> | ||
|
||
<%= render Shared::Stuff.new(wild: 123) do %> | ||
_ya think?_ | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
class PageMe < Bridgetown::Component | ||
include Bridgetown::Viewable | ||
|
||
def initialize(title:) # rubocop:disable Lint/MissingSuper | ||
@title = title.upcase | ||
|
||
data.title = @title | ||
end | ||
|
||
def call(app) | ||
@port_number = app.request.port | ||
|
||
render_with(app) do | ||
layout :page | ||
page_class "some-extras" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class Shared::Stuff < Bridgetown::Component | ||
def initialize(wild:) # rubocop:disable Lint/MissingSuper | ||
@wild = wild * 2 | ||
end | ||
|
||
def template | ||
"Well that was #{@wild}!#{content}" | ||
end | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
title: So Awesome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<title><%= data.title %> | <%= site.metadata.title %></title> | ||
|
||
<body class="<%= data.layout %> <%= data.page_class %>"> | ||
<%= yield %> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
layout: default | ||
--- | ||
|
||
<h1><%= data.title %></h1> | ||
|
||
<%= yield %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters