How to change the root path based on selected theme inside a view

I am currently using Phoenix.Template.render(ChatWeb.ThemeView, "chat_window_live_component", "html", assigns) to render the HTML for all my components. In my view class (ChatWeb.ThemeView), I have the following structure:

elixirCopy code

defmodule ChatWeb.ThemeView do
  use ChatWeb, :html
  embed_templates "**/*", root: "templates/default/"
end

I am facing an issue where I need to change the root path dynamically based on a selected theme. Unfortunately, I cannot find a way to add a condition based on an assign or a socket value.

Problem:

I need to change the root path dynamically in the embed_templates function based on a selected theme. For example, if the user selects the “dark” theme, I want the root path to be “templates/dark/”. If they select the “light” theme, I want it to be “templates/light/”.

Request for Support:

I need assistance in finding a solution to dynamically change the root path in the embed_templates function based on a selected theme. Is there a way to achieve this using assigns or socket values?

What you’re asking for is not possible. root is used to find templates to compile into the module. Once the module is compiled the template files are no longer needed, all their functionality has become functions on the module.

Therefore there’s no way to adjust “templates” at runtime. At runtime there’s just functions on modules.

To make theming work you need to look for different approaches.

Yeah to echo what @LostKobrakai said wholly changing out templates just for theming seems very heavy weight. Normally you can do this via a single CSS class addition at some root DOM node.