Creating a Rate Limiter – Live Stream Archive IntelliJ Elixir v11.1.0

Sharing Phoenix templates across controllers & views

I published a new tutorial: https://alchemist.camp/articles/shared-phoenix-templates

Whenever you use Phoenix’s generators to create a full HTML scaffold, you’ll see that in the template directory, there’s a form.html.eex “partial” or sub-template that’s used inside of both the new.html.eex and the edit.html.eex.

The entire form is embedded inside those new and action templates with just this single line:

<%= render “form.html”, Map.put(assigns, :action, Routes.article_path(@conn, :create)) %>

You can use the same pattern to embed any eex template into another in the same directory. But some templates are things you’d want to render from any template. An email list sign-up form, for example, might appear within a blog post, an about page and many other parts of the site.

This tutorial goes through how to make a clean setup for these “shared templates” so it’s just as easy to create and use them as any other template.