Class: Spree::Emails::Renderer

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/emails/renderer.rb

Overview

Renders an email template into its subject, HTML and plain-text parts.

Liquid fills in the data first, into the template and then the layout wrapping it; MJML then compiles the result into email-safe HTML. Every output is HTML-escaped unless a template marks it | raw, and a runaway template fails its one email rather than stalling the job queue.

Constant Summary collapse

LAYOUT =
'layouts/spree/base_mailer'.freeze
RESOURCE_LIMITS =
{
  render_length_limit: 2_000_000,
  render_score_limit: 200_000,
  assign_score_limit: 50_000
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver:, store:, currency: nil, strict: Rails.env.local?, branding: {}) ⇒ Renderer

Returns a new instance of Renderer.

Parameters:

  • resolver (Spree::Emails::TemplateResolver)
  • store (Spree::Store)
  • currency (String, nil) (defaults to: nil) —

    what money formats in, defaults to the store's

  • strict (Boolean) (defaults to: Rails.env.local?) —

    whether an unknown variable raises; defaults to true in development and test, false in production

  • branding (Hash) (defaults to: {}) —

    unsaved branding values over the store's, for previews



41
42
43
44
45
46
47
# File 'app/services/spree/emails/renderer.rb', line 41

def initialize(resolver:, store:, currency: nil, strict: Rails.env.local?, branding: {})
  @resolver = resolver
  @store = store
  @branding = branding
  @currency = currency.presence || store.default_currency
  @strict = strict
end

Class Method Details

.environment ⇒ Liquid::Environment

The Liquid environment every email renders in: strict parsing and Spree's filters, isolated from any other Liquid the host app runs.

Returns:

  • (Liquid::Environment)


23
24
25
26
27
28
29
30
31
32
# File 'app/services/spree/emails/renderer.rb', line 23

def environment
  @environment ||= Liquid::Environment.build(error_mode: :strict) do |environment|
    environment.register_filter(Spree::Emails::Filters)
    # `include` takes a variable as the partial name, which would echo
    # data back in its error message; `render` only takes a fixed name.
    environment.tags.delete('include')
    environment.register_tag('capture', Spree::Core::Emails::EscapedOutput::Capture)
    environment.register_tag('cycle', Spree::Core::Emails::EscapedOutput::Cycle)
  end
end

Instance Method Details

#render(template, assigns = {}) ⇒ Spree::Emails::RenderedEmail

Parameters:

  • template (Spree::Emails::Template) —

    a Liquid template

  • assigns (Hash) (defaults to: {}) —

    the template's variables

Returns:



52
53
54
55
56
57
58
59
# File 'app/services/spree/emails/renderer.rb', line 52

def render(template, assigns = {})
  assigns = variables(assigns)
  subject = subject_for(template, assigns)
  body = render_liquid(template.body, assigns)
  html = render_layout(body, assigns.merge('subject' => subject))

  Spree::Emails::RenderedEmail.new(subject: subject, html: html, text: render_text(template, assigns, html))
end

#variables(assigns = {}) ⇒ Hash

Everything a template receives: the given variables plus store and locale, with string keys.

Parameters:

  • assigns (Hash) (defaults to: {})

Returns:

  • (Hash)


78
79
80
# File 'app/services/spree/emails/renderer.rb', line 78

def variables(assigns = {})
  base_assigns.merge(assigns.deep_stringify_keys)
end

#wrap(html, subject: nil) ⇒ String

Wraps HTML a mailer rendered some other way — its own ERB views — in the email layout, so it carries the store's logo, header and footer.

Parameters:

  • html (String) —

    the email's body, trusted as rendered

  • subject (String, nil) (defaults to: nil)

Returns:

  • (String) —

    the finished email HTML



67
68
69
70
71
# File 'app/services/spree/emails/renderer.rb', line 67

def wrap(html, subject: nil)
  body = %(<mj-section><mj-column><mj-text align="left">#{html}</mj-text></mj-column></mj-section>)

  render_layout(body, variables('subject' => subject.to_s))
end