5
部分原因是來自方便的反應,使得模板代碼與組件分離很少。以下是我迄今爲止:如何在.rb文件(無模板文件)內渲染郵件程序模板
# app/mailers/membership_mailer.rb
# frozen_string_literal: true
class MembershipMailer < ApplicationMailer
def one_week_expiration(member, renewal, org)
template = slim(%(
= content_for :header
= org.name
Hello, #{member.name},
br
Your membership for #{org.name} is set to expire at
#{renewal.expires_at.to_s(:short)}.
br
Please visit <a href=#{org.url}>#{org.url}</a> to purchase
a membership renewal.
br
br
))
subject = "Your membership for #{org.name} is going to expire soon"
mail(to: member.email, subject: subject) do |format|
# there isn't a slim renderer
format.html { render text: template }
end
end
end
我使用這個佈局我所有的電子郵件,並且它設置爲默認佈局application_mailer。
# app/views/layouts/email.html.slim
= stylesheet_link_tag "email"
header
.shell
h1 = yield :header
.shell
br
= yield :body
= yield
= render partial: "/shared/email_footer"
我想在這裏所有的渲染方法,但我正在掙扎是在哪裏可以找到/弄清楚如何打造出來的郵件程序,佈局模板,並擁有所有我想要的變量可訪問傳遞給模板。
# app/mailers/application_mailer.rb
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: APPLICATION_CONFIG['support_email']
layout 'email'
# taken from slim/test/block rendering/helper
def slim(source, options = {}, &block)
scope = options.delete(:scope)
locals = options.delete(:locals)
Slim::Template.new('', {}) { source }.render(scope, locals, &block)
end
end
最後,我只好再培訓局,ARBRE等
因此,總的來說,我當我撥打電話到郵件(...){...},我喜歡能夠在ruby中定義我的模板,而不是在模板文件中定義,因爲我不喜歡讓郵件程序和模板相距甚遠(在文件系統中......爲什麼超出這個問題的範圍,我現在只想解決紅寶石渲染問題)。