Class: Bridgetown::RubyTemplateView

Inherits:
Object
  • Object
show all
Includes:
Streamlined
Defined in:
bridgetown-core/lib/bridgetown-core/helpers.rb,
bridgetown-core/lib/bridgetown-core/ruby_template_view.rb

Direct Known Subclasses

ERBView

Defined Under Namespace

Classes: Helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Streamlined

#helper

Methods included from ERBCapture

#capture

Constructor Details

#initialize(convertible) ⇒ RubyTemplateView

Returns a new instance of RubyTemplateView.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 30

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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



86
87
88
89
90
91
92
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 86

def method_missing(method_name, ...)
  if helpers.respond_to?(method_name.to_sym)
    helpers.send(method_name.to_sym, ...)
  else
    super
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



27
28
29
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 27

def content
  @content
end

#layoutObject (readonly)

Returns the value of attribute layout.



27
28
29
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 27

def layout
  @layout
end

#paginatorObject (readonly)

Returns the value of attribute paginator.



27
28
29
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 27

def paginator
  @paginator
end

#resourceObject (readonly) Also known as: page

Returns the value of attribute resource.



27
28
29
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 27

def resource
  @resource
end

#siteObject (readonly)

Returns the value of attribute site.



27
28
29
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 27

def site
  @site
end

Instance Method Details

#collectionsObject



60
61
62
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 60

def collections
  site.collections
end

#dataObject



43
44
45
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 43

def data
  resource.data
end

#helpersObject



82
83
84
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 82

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

#inspectObject



98
99
100
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 98

def inspect
  "#<#{self.class} layout=#{layout&.label} resource=#{resource.relative_path}>"
end

#liquid_render(component, options = {}, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 68

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.deep_stringify_keys, _liquid_context).html_safe
end

#partial(_partial_name = nil, **_options) ⇒ Object



47
48
49
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 47

def partial(_partial_name = nil, **_options)
  raise "Must be implemented in a subclass"
end

#render(item, **options) ⇒ Object



51
52
53
54
55
56
57
58
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 51

def render(item, **options, &)
  if item.respond_to?(:render_in)
    result = item.render_in(self, &)
    result&.html_safe
  else
    partial(item, **options, &)&.html_safe
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 94

def respond_to_missing?(method_name, include_private = false)
  helpers.respond_to?(method_name.to_sym, include_private) || super
end

#site_dropObject



64
65
66
# File 'bridgetown-core/lib/bridgetown-core/ruby_template_view.rb', line 64

def site_drop
  site.site_payload.site
end