Module: Bridgetown::Viewable

Includes:
RodaCallable, Transformable
Defined in:
bridgetown-core/lib/bridgetown-core/concerns/viewable.rb

Overview

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

Instance Method Summary collapse

Methods included from Transformable

#transform_content, #transform_with_layout

Methods included from RodaCallable

===

Instance Method Details

#dataObject



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

def data
  @data ||= HashWithDotAccess::Hash.new
end

#front_matter(&block) ⇒ Object



19
20
21
# File 'bridgetown-core/lib/bridgetown-core/concerns/viewable.rb', line 19

def front_matter(&block)
  Bridgetown::FrontMatter::RubyFrontMatter.new(data:).tap { _1.instance_exec(&block) }
end

#relative_pathObject



23
# File 'bridgetown-core/lib/bridgetown-core/concerns/viewable.rb', line 23

def relative_path = self.class.source_location.delete_prefix("#{site.root_dir}/")

#render_in_layout(app) ⇒ Object

Render the component template in the layout specified in your front matter

Parameters:



28
29
30
31
32
33
34
35
36
# File 'bridgetown-core/lib/bridgetown-core/concerns/viewable.rb', line 28

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

#render_with(app) ⇒ Object

Pass a block of front matter and render the component template in layouts

Parameters:



41
42
43
44
# File 'bridgetown-core/lib/bridgetown-core/concerns/viewable.rb', line 41

def render_with(app, &)
  front_matter(&)
  render_in_layout(app)
end

#siteObject



11
12
13
# File 'bridgetown-core/lib/bridgetown-core/concerns/viewable.rb', line 11

def site
  @site ||= Bridgetown::Current.site
end