1

What is the recommended way to differentiate a HTML block in phoenix templating?

I've read the documentation; which draws the following outlines;

  • the root layout is injected by a plug in the router
  • you can optionally set a layout on a route
  • you can optionally set a layout on a view

But is it possible (or otherwise recommended) to override just a template block somehow? For example Django let's you do things like;

base_template

{% block header %}
  <%= render "_header.html", assigns %>
{% endblock %}

{% block content %}
  stuff..
{% endblock %}

inheriting_view_template

{% extends "base_template.html" %}

{% block header %}
  {{ block.super }}
  <p>my super interesting extra header context</p> <-- !
{% endblock %}

{% block content %}
  stuff..
{% endblock %}
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100

1 Answers1

1

So it is something similar to Rails world

some_layout.erb

<html>
  <body>
    <header>
      <%= yield :header %>
    </header>
    <div>
      <%= yield :content %>
    </div>
    <footer>
       <%= yield :footer %>
    </footer>
  </body>
<html>

And just to generalise your answer (for further readers). Two methods possible

render_existing Renders a template only if it exists.

<head>
  <%= render_existing view_module(@conn), "scripts.html", assigns %>
</head>

render_layout Renders the given layout passing the given do/end block as @inner_content.

# layout/blog.html.eex
<%= render_layout LayoutView, "app.html", assigns do %>
  <div class="sidebar">...</div>
  <%= @inner_content %>
<% end %>
zhisme
  • 2,368
  • 2
  • 19
  • 28