Class: Bridgetown::RubyTemplateView

Inherits:
Object
  • Object
show all
Includes:
Streamlined
Defined in:
lib/bridgetown-core/helpers.rb,
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.



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

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



86
87
88
89
90
91
92
93
94
# File '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, ...)
  elsif data_key?(method_name, ...)
    data[method_name]
  else
    super
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#layoutObject (readonly)

Returns the value of attribute layout.



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

def layout
  @layout
end

#paginatorObject (readonly)

Returns the value of attribute paginator.



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

def paginator
  @paginator
end

#resourceObject (readonly) Also known as: page

Returns the value of attribute resource.



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

def resource
  @resource
end

#siteObject (readonly)

Returns the value of attribute site.



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

def site
  @site
end

Instance Method Details

#collectionsObject



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

def collections = site.collections

#dataObject



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

def data = resource.data

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

Returns:

  • (Boolean)


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

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



76
77
78
# File 'lib/bridgetown-core/ruby_template_view.rb', line 76

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

#inspectObject



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

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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bridgetown-core/ruby_template_view.rb', line 62

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



51
# File 'lib/bridgetown-core/ruby_template_view.rb', line 51

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

#render(item, **options) ⇒ Object



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

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)


96
97
98
# File 'lib/bridgetown-core/ruby_template_view.rb', line 96

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

#site_dropObject



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

def site_drop = site.site_payload.site