bamboo v0.7.0 Bamboo.Phoenix
Render templates and layouts with Phoenix.
This module makes it very easy to render layouts and views using Phoenix.
Pass an atom (e.g. :welcome_email
) as the template name to render HTML and
plain text emails. Use a string if you only want to render one type, e.g.
"welcome_email.text"
or "welcome_email.html"
.
Examples
defmodule Email do
use Bamboo.Phoenix, view: MyApp.EmailView
def text_and_html_email_with_layout do
new_email()
# You could set just a text layout or just an html layout
|> put_text_layout({MyApp.LayoutView, "email.text"})
|> put_html_layout({MyApp.LayoutView, "email.html"})
# Or you can set a layout for both html and text at the same time
|> put_layout({MyApp.LayoutView, :email})
# Pass an atom to render html AND plain text templates
|> render(:text_and_html_email)
end
def text_and_html_email_without_layouts do
new_email()
|> render(:text_and_html_email)
end
def email_with_assigns(user) do
new_email()
# @user will be available in the template
|> render(:email_with_assigns, user: user)
end
def email_with_already_assigned_user(user) do
new_email()
# @user will be available in the template
|> assign(:user, user)
|> render(:email_with_assigns)
end
def html_email do
new_email
|> render("html_email.html")
end
def text_email do
new_email
|> render("text_email.text")
end
end
HTML Layout Example
# web/email.ex
defmodule Myapp.Email do
use Bamboo.Phoenix, view: Myapp.EmailView
def sign_in_email(person) do
base_email
|> to(person)
|> subject("Your Sign In Link")
|> assign(:person, person)
|> render(:sign_in)
end
defp base_email do
new_email
|> from("Rob Ot<robot@changelog.com>")
|> put_header("Reply-To", "editors@changelog.com")
# This will use the "email.html.eex" file as a layout when rendering html emails.
# Plain text emails will not use a layout unless you use `put_text_layout`
|> put_html_layout({Myapp.LayoutView, "email.html"})
end
end
# web/views/email_view.ex
defmodule Myapp.EmailView do
use Myapp.Web, :view
end
# web/templates/layout/email.html.eex
<html>
<head>
<link rel="stylesheet" href="<%= static_url(Myapp.Endpoint, "/css/email.css") %>">
</head>
<body>
<%= render @view_module, @view_template, assigns %>
</body>
</html>
# web/templates/email/sign_in.html.eex
<p><%= link "Sign In", to: sign_in_url(Myapp.Endpoint, :create, @person) %></p>
# web/templates/email/sign_in.text.eex
# This will not be rendered within a layout because `put_text_layout` was not used.
Sign In: <%= sign_in_url(Myapp.Endpoint, :create, @person) %>
Summary
Functions
Sets an assign for the email. These will be availabe when rendering the email
Sets the layout when rendering HTML templates
Sets the layout for rendering plain text and HTML templates
Sets the layout when rendering plain text templates
Render a Phoenix template and set the body on the email
Functions
Sets an assign for the email. These will be availabe when rendering the email
Sets the layout when rendering HTML templates
Example
def html_email_layout do
new_email
# Will use MyApp.LayoutView with email.html template when rendering html emails
|> put_html_layout({MyApp.LayoutView, "email.html"})
end
Sets the layout for rendering plain text and HTML templates
Example
def text_and_html_email_layout do
new_email
# Will use MyApp.LayoutView with the email.html template for html emails
# and MyApp.LayoutView with the email.html template for text emails
|> put_layout({MyApp.LayoutView, :email})
end
Sets the layout when rendering plain text templates
Example
def text_email_layout do
new_email
# Will use MyApp.LayoutView with email.text template when rendering text emails
|> put_text_layout({MyApp.LayoutView, "email.text"})
end
Render a Phoenix template and set the body on the email
Pass an atom as the template name to render HTML and plain text emails,
e.g. :welcome_email
. Use a string if you only want to render one type, e.g.
"welcome_email.text"
or "welcome_email.html"
. Scroll to the top for more examples.