kbee

kbee

How do i dynamically theme Phoenix views

Hello community,

I’m new to phoenix and struggling to implement a feature that would allow my users to apply a theme of their choice (e.g. animals, cars, nature).

Each of these themes has its own assets and I’m using put_layout to assign the chosen layout but struggling to render the views according to the chosen theme

My controller looks like this

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

    conn
    |> put_layout(theme)
    |> render('index.html')
  end
end 

My templates directory

templates\
 -> layouts
     - app.html.eex
     - themeA.html.eex
     - themeB.html.eex
 -> pages
     - index.html.eex
 -> themeA
     - index.html.eex
 -> themeB
     - index.html.eex

how do i dynamically choose between themeA/index.html.eex and themeB/index.html.eex without getting a template not found error

First 4 of 4 Posts Switch mode

kokolegorille

kokolegorille

Hello and welcome,

I think it should be…

|> put_layout(theme)

as theme is your variable.

kbee

kbee OP

thanks for catching the type @kokolegorille. The layout part works ok. it’s dynamically rendering the template I’m having trouble with. See the example below

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

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

kokolegorille

If You are using PageController, You are using PageView, which will look into template/page.

Try to put themes folder inside page folder.

kbee

kbee OP

@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://forum.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 
— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement