Class: Bridgetown::RubyTemplateView

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

Constructor Details

#initialize(convertible) ⇒ RubyTemplateView

Returns a new instance of RubyTemplateView.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bridgetown-core/ruby_template_view.rb', line 12

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, *args, **kwargs, &block) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/bridgetown-core/ruby_template_view.rb', line 68

def method_missing(method_name, *args, **kwargs, &block)
  if helpers.respond_to?(method_name.to_sym)
    helpers.send method_name.to_sym, *args, **kwargs, &block
  else
    super
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/bridgetown-core/ruby_template_view.rb', line 9

def content
  @content
end

#layoutObject (readonly)

Returns the value of attribute layout.



9
10
11
# File 'lib/bridgetown-core/ruby_template_view.rb', line 9

def layout
  @layout
end

#paginatorObject (readonly)

Returns the value of attribute paginator.



9
10
11
# File 'lib/bridgetown-core/ruby_template_view.rb', line 9

def paginator
  @paginator
end

#resourceObject (readonly) Also known as: page

Returns the value of attribute resource.



9
10
11
# File 'lib/bridgetown-core/ruby_template_view.rb', line 9

def resource
  @resource
end

#siteObject (readonly)

Returns the value of attribute site.



9
10
11
# File 'lib/bridgetown-core/ruby_template_view.rb', line 9

def site
  @site
end

Instance Method Details

#collectionsObject



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

def collections
  site.collections
end

#dataObject



25
26
27
# File 'lib/bridgetown-core/ruby_template_view.rb', line 25

def data
  resource.data
end

#helpersObject



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

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

#inspectObject



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

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

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



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

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



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

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

#render(item, **options, &block) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#site_dropObject



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

def site_drop
  site.site_payload.site
end