How to dynamically invoke an Embedded Template?

Hey Guys,

I’m trying to move my blog posts from Ghost to Phoenix Live View.

I am aware of 2 approaches, that others take, to create blogs.

  1. Nimble Publisher (That compiles the markdown to HTML, and uses the file system as storage)
  2. Postgres/Sqlite backed one, where new posts are added to the database.

However, I want to go the third route, which is just writing my blog posts in HEEX.

This is what I have tried so far:

defmodule DerpyToolsWeb.BlogLive do
  use DerpyToolsWeb, :live_view

  embed_templates "posts/*"

  def mount(%{"post_slug" => post_slug}, _session, socket) do
    IO.inspect(post_slug, label: "post_slug")
    {:ok, socket}

  def render(assigns) do
    <.taskfile_a_sensible_makefile_and_shell_script_alternative />
    <%!-- <.live_component
      module={taskfile_a_sensible_makefile_and_shell_script_alternative}  # How to make this work dynamically, using a string?
    /> --%>

So, how to embed that live component based on a string received from URL?

  1. Do I use something other than live_component?
  2. Will embed_templates import, say 1000 blog posts from the folder? (Is it bad? Although I doubt I will ever write that many blog posts. :sweat_smile:)

This seems kind of a strange approach, wouldn’t it be smarter to do this upper in the pipeline, say at the router level?

@LostKobrakai has already done this in his blog and has open sourced the approach.