Amu

Amu

Hi. I am developing single page blog website, using LiveView.

I want to render html template according to URL path .

For example

blog directory has many html files as “blog posts” like as below

blog/post-1.html.heex

blog/post-2.html.heex
.
.
.
blog/post-100.html.heex

If I access http://localhost:4000/blog/post-1

I want to get post-1.html as LiveView.

Perhaps I need to set route, but I am not sure.

live “/blog/:post”, BlogLive.Index, :index

And I need to use ‘embed_templates’ perhaps.

Showing Posts 1 to 4

ollien

ollien

Before I answer your question: I just want to ask if using a LiveView and a functional component is the most appropriate tool for this? You would be better served with these files being rendered to disk as plain HTML (or some other templating language, like Mustache), and serving those files directly (or applying your templating function of choice). See this post for some more details: How can one render a Liveview .heex file dynamically? - #2 by benwilson512

However, if you truly understand what you are doing (or just want to learn :slight_smile: ), you can do what you want using Kernel.apply/3. Again, I would not suggest this; you’re effectively allowing a requester to select a function for them to execute. Further, HEEx templates are arbitrarily powerful, and a setup like this could be used for arbitrary code execution on your server, given the right circumstances.

To set this up in the safest way possible (again, I would not consider this really that safe), I split out the components into a separate PageHTML module. Here’s the associated directory structure.

.
├── page.ex
├── page_html
│   ├── page-1.heex
│   └── page-2.heex
└── page_html.ex

PageHTML is quite simple in that it just uses embed_templates, as you suggest

defmodule PhxtestWeb.PageHTML do
  use PhxtestWeb, :html

  embed_templates "page_html/*"
end

I defined a route for the live view as

live "/:page", PageLiveView

…which renders the following live view

defmodule PhxtestWeb.PageLiveView do
  alias PhxtestWeb.PageHTML
  use PhxtestWeb, :live_view

  def render(assigns) do
    with {:ok, template_atom} <- atom_for_page(assigns.page),
         {:ok, rendered} <- try_component_render(template_atom, assigns) do
           rendered
    else
     {:error, :notfound} -> ~H"""
     <span>Not found.</span>
     """
    end
  end

  def mount(%{"page" => page}, _session, socket) do
    {:ok, assign(socket, :page, page)}
  end

  defp atom_for_page(page) do
    try do
      # By using String.to_existing_atom, we ensure that we are not leaking atoms
      # with arbitrary input from a user
      {:ok, String.to_existing_atom(page)}
    rescue
      # String.to_existing_atom throws an ArgumentError if the atom is not defined
      # The atom will be defined at function-definition time for the template 
      # (`embed_templates` effectively just generates a function definition for you, AFAIK)
      ArgumentError -> {:error, :notfound}
    end
  end

  defp try_component_render(component_name, assigns) do
    try do
      rendered = apply(PageHTML, component_name, [assigns])

      {:ok, rendered}
    rescue
       # apply throws an ArgumentError if the function is not found... 
       # this would technically also happen if the component threw an 
       # ArgumentError, but this isn't production quality code to begin with :)
      ArgumentError -> {:error, :notfound}
    end
  end
end

Now, when someone navigates to /page-1, the page-1 template will be rendered

and when visiting a template that doesn’t exist, they will get “Not found”

Hope this helps! Now, don’t do this :slight_smile:

Amu

Amu OP

Thank you very much ollien !!
I understand you do not recommend using LiveView for this purpose.
Again, Could you tell me how to render static HTML file accoring to URL PATH without using LiveView.

ollien

ollien

You can either serve it as a static file directly (perhaps my suggested approach if just using plain .html files). This page, though not a direct tutorial, should get you started. Asset Management — Phoenix v1.8.8

You can also use an approach like this to render a string as your controller response, which would be particularly useful if you are using templates https://stackoverflow.com/a/67350254

Amu

Amu OP

you help me a lot !

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews