A template is a file that contains a body for a specific format of a multipart email.
welcome.html.erb describes the markup of the HTML part of the message, while
welcome.txt.erb is for the textual part.
It is rendered by bounding the context of a mailer and using a template engine.
For convenience, there is a correlation between the view mailer name and the template file name.
It’s the translation of the name into a path: from
The remaining part is made of multiple file extensions. The first is relative to the format and the latter is for the template engine.
Hanami only accepts
:txt formats for emails.
For a given mailer named
Mailers::ForgotPassword, there must be at least one template
forgot_password.[format].[engine] under the mailers templates directory.
If we want to associate a different template to a mailer, we can use
# lib/bookshelf/mailers/forgot_password.rb module Mailers class ForgotPassword include Hanami::Mailer template 'send_password' end end
Our view will look for
Hanami looks at the last extension of a template file name to decide which engine to use (eg
welcome.html.erb will use ERb).
The builtin rendering engine is ERb, but Hanami supports countless rendering engines out of the box.
This is a list of the supported engines.
They are listed in order of higher precedence, for a given extension.
For instance, if ERubis is loaded, it will be preferred over ERb to render
|Erubis||erb, rhtml, erubis|
|Redcarpet||markdown, mkd, md|
|RDiscount||markdown, mkd, md|
|Kramdown||markdown, mkd, md|
|Maruku||markdown, mkd, md|
|BlueCloth||markdown, mkd, md|
|Asciidoctor||ad, adoc, asciidoc|
|WikiCloth||wiki, mediawiki, mw|
In order to use a different template engine we need to bundle the gem and to use the right file extension.
# lib/bookshelf/mailers/templates/welcome.html.haml %h1 Welcome
Templates are located in the default directory
mailers/templates, located under an application’s directory
bookshelf is the name of our application.
If we want to customize this location, we can set a different value in Hanami::Mailer configuration.
# lib/bookshelf.rb # ... Hanami::Mailer.configure do # ... root 'path/to/templates' end.load!
The application will now look for templates under