Cervajz

Cervajz

Hello,

I’d like to use Phoenix function component but without the HEEX code being part of the module’s code - the HTML template is pretty long to be included in the Elixir module itself and would make the navigation and editing expierence somewhat hard.

Is there any way I could render the HTML from an external .html.heex file from within the function body, please?

Something like this

defmodule DashboardWeb.Components.Navigation do
  use Phoenix.Component

  def main_navigation(assigns) do
    # Render .html.heex file here instead of the ~H sigil
  end
end

Directory structure:

I haven’t found any information in the documentation or in the source code and I failed all of my tries rendering the file view Phoenix.View or LiveView or even via Phoenix.Template :sweat_smile:

In Phoenix.LiveComponent this was automatically done when the render function was not defined in the component module (via Phoenix.LiveView.Renderer.before_compile) but I can’t figure out how to elegantly do it in the function component world.

Thank you and have a nice one :slight_smile:

Showing Posts 1 to 10

Cervajz

Cervajz OP

For the context: The reason I am migrating standard LiveView components to the function ones is that I’d like to reuse some of the templates in “dead views” as well - mainly the ones generated by phx gen.auth in Phoenix 1.6.0. The rest of the application is all in LiveView where I can stay with standard LiveView components.

Is my approach wrong? Should I just duplicate the templates and use standard Phoenix.View in the “dead view” part of the application?

seb3s

seb3s

Hello,
sure it functions

I do this in my code :

defmodule VtmeetWeb.Component do
  use Phoenix.Component
 
  def avatar(assigns) do
    Phoenix.View.render(VtmeetWeb.LiveView, "component/avatar.html", assigns)
  end
end

And my dir is
image

Hope this helps,
cheers
Sébastien

Cervajz

Cervajz OP

Thank you.

May I see how the VtmeetWeb.LiveView module looks like, please? Is that some kind of “root” module for the whole application?

I’d like to use the Phoenix.Component outside of specific LiveView context (page) . Basically rendering it in the templates/layout/live.html.heex file like so:

<main role="main">
  <DashboardWeb.Components.Navigation.main_navigation current_user={@current_user} />
  <%= @inner_content %>
</main>

and similarly in the templates/layout/app.html.heex template

seb3s

seb3s

Currently the components I used are within liveview. So I’ve not tried what you want to accomplish.

Here is my liveview module

defmodule VtmeetWeb.LiveView do
  use VtmeetWeb, :subtemplate_view
end

and the subtemplate_view def within vtmeet_web.ex :

  def subtemplate_view do
    quote do
      use Phoenix.View,
        root: "lib/vtmeet_web",
        namespace: VtmeetWeb,
        pattern: "**/*"

      # Import convenience functions from controllers
      import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1]

      # Include shared imports and aliases for views
      unquote(view_helpers())
    end
  end

Note the pattern which allow to find the template in subdirectories (maybe that’s what is missing in your case).

Sébastien.

Cervajz

Cervajz OP

That somewhat helped but I am unable to render the template w/o a LiveView context as it needs @socket variable. Which means the function component in this form is unfortunately not usable in the dead layout (aka app.html.heex)

For now I decided to just duplicate the navigation HTML template for the non-LV part of the application.

Thank you for you help <3

Cervajz

Cervajz OP

Correction: I was actually able to make it work with the solution provided by @seb3s. :tada:

The @socket variable was not needed at all.

Thank you!

APB9785

APB9785

Creator of ECSx

This is an interesting approach, but I don’t think it’s right. It works ok for a single type of component with a single template, but as soon as I added multiple components, each with their own template, the compiler gave some strange errors saying the view module couldn’t be found.

It seems like there should be a simpler way, since stateful components have this behavior automatically.

josevalim

josevalim

Creator of Elixir

Yes, in a nutshell this feature is not yet supported.

thiagomajesk

thiagomajesk

@josevalim Will this be added to the heex roadmap? Can’t find if this is being tracked.

RooSoft

RooSoft

I was also trying to separate HEEX from the .ex file. The main reason was because Visual Studio Code doesn’t seem to handle intellisense pretty well in my current setup.

Since that separation doesn’t seem to be supported right now.. if anybody’s got intellisense to recognize CSS classes, I’d be interested in knowing which extensions your using… along with any extra configuration that you made to get there.

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews