Class: Bridgetown::TemplateView
- Inherits:
-
Object
- Object
- Bridgetown::TemplateView
- Includes:
- Streamlined
- Defined in:
- bridgetown-core/lib/bridgetown-core/template_view.rb,
bridgetown-core/lib/bridgetown-core/helpers.rb
Overview
Holds context for page rendering and is subclassed by various template engines like ERB.
Direct Known Subclasses
Defined Under Namespace
Classes: Helpers
Class Attribute Summary collapse
-
.extname_list ⇒ Object
Returns the value of attribute extname_list.
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#layout ⇒ Object
readonly
Returns the value of attribute layout.
-
#paginator ⇒ Object
readonly
Returns the value of attribute paginator.
-
#resource ⇒ Object
(also: #page)
readonly
Returns the value of attribute resource.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Class Method Summary collapse
-
.input(extnames) ⇒ Object
View renderers can provide one or more extensions they accept.
-
.new_with_data(virtual_path = "VIRTUAL", **data) ⇒ TemplateView
Creates a new virtual TemplateView with a “resource” containing the provided data.
-
.render ⇒ String
Calls the render method of a global TemplateView which lets you output partials and components outside of a specific rendering context.
-
.virtual_view ⇒ TemplateView
Returns a global “virtual” TemplateView for use in rendering out side of a specific context.
Instance Method Summary collapse
-
#collections ⇒ Object
-
#data ⇒ Object
-
#data_key?(key, *args, **kwargs) ⇒ Boolean
-
#helpers ⇒ Object
-
#initialize(convertible) ⇒ TemplateView
constructor
A new instance of TemplateView.
-
#inspect ⇒ Object
-
#liquid_render(component, **options, &block) ⇒ Object
-
#method_missing(method_name) ⇒ Object
-
#partial(partial_name = nil, **options) ⇒ Object
-
#render(item) { ... } ⇒ String
Renders a partial, or a component object which responds to
render_in. -
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#site_drop ⇒ Object
-
#template_view_classes ⇒ Object
Methods included from Streamlined
Methods included from ERBCapture
Constructor Details
#initialize(convertible) ⇒ TemplateView
Returns a new instance of TemplateView.
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 84 def initialize(convertible) if convertible.is_a?(Layout) @layout = convertible @resource = layout.current_document @content = layout.current_document_output else @layout = convertible.site.layouts[convertible.data["layout"]] @resource = convertible end @paginator = resource.paginator if resource.respond_to?(:paginator) @site = resource.site @support_data_as_view_methods = @site.config.support_data_as_view_methods end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 161 def method_missing(method_name, ...) if helpers.respond_to?(method_name.to_sym) helpers.send(method_name.to_sym, ...) elsif data_key?(method_name, ...) data[method_name] else super end end |
Class Attribute Details
.extname_list ⇒ Object
Returns the value of attribute extname_list.
33 34 35 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 33 def extname_list @extname_list end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
29 30 31 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 29 def content @content end |
#layout ⇒ Object (readonly)
Returns the value of attribute layout.
29 30 31 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 29 def layout @layout end |
#paginator ⇒ Object (readonly)
Returns the value of attribute paginator.
29 30 31 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 29 def paginator @paginator end |
#resource ⇒ Object (readonly) Also known as: page
Returns the value of attribute resource.
29 30 31 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 29 def resource @resource end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
29 30 31 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 29 def site @site end |
Class Method Details
.input(extnames) ⇒ Object
View renderers can provide one or more extensions they accept. Examples:
input :erbinput %i(rb ruby)
41 42 43 44 45 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 41 def input(extnames) extnames = Array(extnames) self.extname_list ||= [] self.extname_list += extnames.map { |e| ".#{e.to_s.downcase}" } end |
.new_with_data(virtual_path = "VIRTUAL", **data) ⇒ TemplateView
Creates a new virtual TemplateView with a “resource” containing the provided data
66 67 68 69 70 71 72 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 66 def new_with_data(virtual_path = "VIRTUAL", **data) res = Bridgetown::Model::Base.build( { site: Bridgetown::Current.site }.as_dots, :pages, virtual_path, data ).to_resource.read! new(res) end |
.render ⇒ String
Calls the render method of a global TemplateView which lets you output partials and components outside of a specific rendering context
81 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 81 def render(...) = virtual_view.render(...) |
.virtual_view ⇒ TemplateView
Returns a global “virtual” TemplateView for use in rendering out side of a specific context
51 52 53 54 55 56 57 58 59 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 51 def virtual_view # if site object has changed, clear previous state @virtual_res = @virtual_view = nil if @virtual_res&.site != Bridgetown::Current.site @virtual_res ||= Bridgetown::Model::Base.build( { site: Bridgetown::Current.site }.as_dots, :pages, "VIRTUAL", {} ).to_resource.read! @virtual_view ||= new(@virtual_res) end |
Instance Method Details
#collections ⇒ Object
100 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 100 def collections = site.collections |
#data ⇒ Object
98 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 98 def data = resource.data |
#data_key?(key, *args, **kwargs) ⇒ Boolean
155 156 157 158 159 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 155 def data_key?(key, *args, **kwargs) return false unless @support_data_as_view_methods args.empty? && kwargs.empty? && !block_given? && data.key?(key) end |
#helpers ⇒ Object
151 152 153 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 151 def helpers @helpers ||= Helpers.new(self, site) end |
#inspect ⇒ Object
175 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 175 def inspect = "#<#{self.class} layout=#{layout&.label} resource=#{resource.relative_path}>" |
#liquid_render(component, **options, &block) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 127 def liquid_render(component, **, &block) [:_block_content] = capture(&block) if block && respond_to?(:capture) render_statement = _render_statement(component, ) template = site.liquid_renderer.file( "#{resource.path}.#{Digest::SHA2.hexdigest(render_statement)}" ).parse(render_statement) template.warnings.each do |e| Bridgetown.logger.warn "Liquid Warning:", LiquidRenderer.format_error(e, path || document.relative_path) end template.render!(.as_dots, _liquid_context).html_safe end |
#partial(partial_name = nil, **options) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 141 def partial(partial_name = nil, **, &) partial_name = [:template] if partial_name.nil? && [:template] found_file = _locate_partial(partial_name) view_class = _view_class_for_partial(found_file) view_class.new(resource).tap do |view| view.resource.roda_app = self.class.virtual_view.resource.roda_app end.partial(partial_name, **, &) end |
#render(item) { ... } ⇒ String
Renders a partial, or a component object which responds to render_in
118 119 120 121 122 123 124 125 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 118 def render(item, **, &) if item.respond_to?(:render_in) result = item.render_in(self, &) result&.html_safe else partial(item, **, &)&.html_safe end end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
171 172 173 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 171 def respond_to_missing?(method_name, include_private = false) helpers.respond_to?(method_name.to_sym, include_private) || super end |
#site_drop ⇒ Object
102 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 102 def site_drop = site.site_payload.site |
#template_view_classes ⇒ Object
104 105 106 107 108 109 110 |
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 104 def template_view_classes @template_view_classes ||= TemplateView.descendants.each_with_object({}) do |klass, hsh| klass.extname_list.each do |ext| hsh[ext] = klass end end end |