Method: Webhookdb::Message.render

Defined in:
lib/webhookdb/message.rb

.render(template, transport_type, recipient) ⇒ Object

Render the transport-specific version of the given template and return a the rendering (content and exposed variables).

Templates can expose data to the caller by using the ‘expose’ tag, like expose subject %Hello from Webhookdb{% endexpose %}. This is available as [:subject] on the returned rendering.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/webhookdb/message.rb', line 67

def self.render(template, transport_type, recipient)
  template_file = template.template_path(transport_type)
  raise MissingTemplateError, "#{template_file} does not exist" unless template_file.exist?

  drops = template.liquid_drops.stringify_keys.merge(
    "recipient" => Webhookdb::Message::CustomerDrop.new(recipient),
    "environment" => Webhookdb::Message::EnvironmentDrop.new,
    "app_url" => Webhookdb.app_url,
  )
  drops = self.unify_drops_encoding(drops)

  content_tmpl = Liquid::Template.parse(template_file.read)
  # The 'expose' drop smashes data into the register.
  # We need to keep track of the register to get the subject back out,
  # so we need to make our own context.
  lctx = Liquid::Context.new(
    [drops, content_tmpl.assigns],
    content_tmpl.instance_assigns,
    content_tmpl.registers,
    true,
    content_tmpl.resource_limits,
  )
  content = content_tmpl.render!(lctx, strict_variables: true)

  transport = Webhookdb::Message::Transport.for(transport_type)
  if transport.supports_layout?
    layout_file = template.layout_path(transport_type)
    if layout_file
      raise MissingTemplateError, "#{template_file} does not exist" unless layout_file.exist?
      layout_tmpl = Liquid::Template.parse(layout_file.read)
      drops["content"] = content.dup
      content = layout_tmpl.render!(drops, strict_variables: true, registers: content_tmpl.registers)
    end
  end

  return Rendering.new(content, lctx.registers)
end