Class: Bridgetown::TemplateView

Inherits:
Object
  • Object
show all
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

ERBView

Defined Under Namespace

Classes: Helpers

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Streamlined

#helper

Methods included from ERBCapture

#capture

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_listObject

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

#contentObject (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

#layoutObject (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

#paginatorObject (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

#resourceObject (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

#siteObject (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 :erb
  • input %i(rb ruby)

Parameters:

  • extnames (Array<Symbol>)

    extensions



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

Parameters:

  • virtual_path (String) (defaults to: "VIRTUAL")

    a fictional “relative URL” for the resource

  • **data (Hash)

    to be passed along to the resource

Returns:



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

.renderString

Calls the render method of a global TemplateView which lets you output partials and components outside of a specific rendering context

Parameters:

Returns:

  • (String)

See Also:

  • {TemplateView{TemplateView#render}


81
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 81

def render(...) = virtual_view.render(...)

.virtual_viewTemplateView

Returns a global “virtual” TemplateView for use in rendering out side of a specific context

Returns:



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

#collectionsObject



100
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 100

def collections = site.collections

#dataObject



98
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 98

def data = resource.data

#data_key?(key, *args, **kwargs) ⇒ Boolean

Returns:

  • (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

#helpersObject



151
152
153
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 151

def helpers
  @helpers ||= Helpers.new(self, site)
end

#inspectObject



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, **options, &block)
  options[:_block_content] = capture(&block) if block && respond_to?(:capture)
  render_statement = _render_statement(component, options)

  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!(options.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, **options, &)
  partial_name = options[:template] if partial_name.nil? && options[: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, **options, &)
end

#render(item) { ... } ⇒ String

Renders a partial, or a component object which responds to render_in

Parameters:

Yields:

  • optional block which is passed along to the component’s render_in method

Returns:

  • (String)


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

Returns:

  • (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_dropObject



102
# File 'bridgetown-core/lib/bridgetown-core/template_view.rb', line 102

def site_drop = site.site_payload.site

#template_view_classesObject



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