Can the root layout template (HTML) be defined via a function instead of a file?

Hello,

While working on a “single-file” Mix.install/2 LiveView sample (a SVG clock), ultimately with the goal to move most of the code inside a single-file (if possible!), I am looking for a way to define the HTML root layout not as a file, but as a function.

So far I could not find how to do that in the documentation.

Does anyone know if this is currently possible in a way or another?

Thanks!

– Thibaut

3 Likes

All templates are precompile as render functions on the view module, so this is all you need:


defmodule MyAppWeb.LayoutView
  use MyAppWeb, :view

  def render("root.html", assigns) do
    ~H"""
    ...
    """
  end
end
5 Likes

Thank you!