jatins

jatins

Render functional component directly in controller without .html.heex template?

the docs for functional components say (emphasis mine)

Although this is a quick example, it shows the different roles function components play in Phoenix:

  • Function components can be defined as functions that receive assigns as argument and call the ~H sigil, as we did in greet/1
  • Function components can be embedded from template files, that’s how we load show.html.heex into HelloWeb.HelloHTML
  • Function components can declare which attributes are expected, which are validated at compilation time
  • Function components can be directly rendered from controllers
  • Function components can be directly rendered from other function components, as we called <.greet messenger={@messenger} /> from show.html.heex

Ask: How do I render a functional components (that is the function returning ~H sigil) directly from controller function without needing a .html.heex template in the middle? Couldn’t find any examples for it in docs

Marked As Solved

sodapopcan

sodapopcan

I think that may be just weird wording referring to how you can do this:

def MyAppWeb.FooController do
  use MyAppWeb, :controller

  def index(conn, _params) do
    render(conn, :index)
  end
end

def MyAppWeb.FooHTML do
  use MyAppWeb, :html

  def index(assigns) do
    ~H"""
    My template
    """
  end
end

def index(assigns) is a component which is rendered from the controller by referencing its name as opposed to “rendering a template from the controller.” AFAIK, you can’t render heex inside a controller action which is what you’re asking. If you have some better wording you could always open a PR to improve!

It’s possible I’m wrong and you can do what you’re asking but I’ve searched through the docs and can’t find anything. render doesn’t take HEEx at all.

Also Liked

jatins

jatins

@sodapopcan thanks, this is what I was looking for! The examples I came across render a .html.heex “template” rather than a component directly. But this answers "how to skip the .html.heex templates. I’ll send a PR to docs to add this example as well

@LostKobrakai that’s interesting! letting you skip the view layer (i,e, MyAppWeb.FooHTML) completely

Last Post!

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement