How to move html code from a component function into an html/heex partial?

Let’s say I have something like this:

    defmodule MyAppWeb.Live.PostComponents do
      use Phoenix.Component
      use Phoenix.HTML

      def card(assigns) do
        ~H"""
        <div class="row ....">
            <nav class="......"></nav>
            <%= live_redirect "...", to: @content.slug, class: "stretched-link" %>
          <%= if  do %>
            blah-blah-blah-blah-blah-blah
          <% end %>
        </div>
        """
      end

How to move this code into an html or *.heex file to then call render(…) somehow?
The end result must remain the same.

Hi @managua1902 the function you are looking for is Phoenix.Component — Phoenix LiveView v0.19.5

render/3 is part of the now optional phoenix_view api. To use it you need to depend on the (now separate) project as well as move back to the old way it included templates. This is also incompatible with the <Module.component /> way of composing function components in heex.

For new projects you’re probably better off using embed_templates, which allows you to switch between function components defined inline or as templates without change in how you call them.

1 Like