How do i dynamically theme Phoenix views

@kokolegorille thanks for your answer. It set me back on track. I was able to get it to to render using by moving and renaming themeA\index.html.eex to page\themeA.index.html.

I wanted to be able to neatly put organise themes in folders and I found this topic https://elixirforum.com/t/how-to-render-a-template-inside-a-web-templates-folder-subfolder/1404/6?u=kbee and changed my view to

defmodule MyApp.PageView do
  use Phoenix.View, root: "lib/myapp/templates", pattern: "**/*"
end

and now I can do

defmodule MyApp.PageController do
  use MyApp, :controller
  
  def index(conn, _params) do
    theme = get_user_theme(conn)

    conn
    |> put_layout(theme)
    |> render('#{theme}/index.html')
  end
end 
3 Likes