Hi
I am using phoenix_markdown to being able to have template.html.md
files, it is working great, I’m happy with that.
I am also using swoosh along with phoenix_swoosh to send emails using templates, like:
use Phoenix.Swoosh, view: Sample.EmailView, layout: {Sample.LayoutView, :email}
def welcome(user) do
new()
|> from("tony@stark.com")
|> to(user.email)
|> subject("Hello, Avengers!")
|> render_body("welcome.html", %{username: user.email})
end
It also works, even if the template is welcome.html.md
or welcome.text.md
, which is pretty cool.
But now, I would like that render_body
generates the html
and the text
format using the same template, and when it is html
it is markdown precompiled, when it is text
it is not (which is what markdown is about, isn’t it?)
I thought I’d create a new format_encoder
that I would call email, so I can create welcome.email.markdown
, which would compile both templates: welcome.html
and welcome.text
.
I looked into what’s going on at the .md
pre-processing, but unfortunately, the entry is the function compile(template_path, template_name)
, which returns the pre-compiled template. I hoped it would return {template_name, compiled_template}
which would be added to the pre-compiled store of the app, but it doesn’t.
How can I achieve what I want, which is to DRY the template content, and that it works for both formats, still being processed, ideally at compile time (at runtime, I shall find a reasonable solution)?
Am I being clear about my question? Do you agree this would make sense? Shouldn’t this even be default?
The two tracks I see are either duplicating the file just before it gets compiled, or tricking the compile(template_path, template_name)
caller so that it produces two compiled templates with each extension.
Anyone can help with this?
Thanks!