Class: Spree::Emails::Renderer
- Inherits:
-
Object
- Object
- Spree::Emails::Renderer
- 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
-
.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.
Instance Method Summary collapse
-
#initialize(resolver:, store:, currency: nil, strict: Rails.env.local?, branding: {}) ⇒ Renderer
constructor
A new instance of Renderer.
- #render(template, assigns = {}) ⇒ Spree::Emails::RenderedEmail
-
#variables(assigns = {}) ⇒ Hash
Everything a template receives: the given variables plus
storeandlocale, with string keys. -
#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.
Constructor Details
#initialize(resolver:, store:, currency: nil, strict: Rails.env.local?, branding: {}) ⇒ Renderer
Returns a new instance of Renderer.
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.
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..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
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.
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.
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 |