annad

annad

How to embed external html file inside of LiveView render

I am using Phoenix Swoosh to generate emails for my app, but I want to show the user what the automated email will look like.

My LiveView render function looks like this:

defp render_welcome_template(assigns) do
    email_data =
      %{email_header: "Moderator has Approved your Request!",
        to_username: "MEMBER_USERNAME",
        group_name: assigns.group_name,
        from_username: assigns.moderator.username,
        reply_link: GroupMail.build_reply_link(assigns.moderator.id, assigns.group_id),
        group_link: GroupMail.build_group_link(assigns.group_id)
      }
   Map.put(assigns, :email_data, email_data)
    ~H"""
      <h2>Edit Welcome Template</h2>
      <div>Below is the email message that will be sent when the moderator approves a new member. </div>

      ***** THIS IS WHERE I WANT TO EMBED THAT HTML BUT THE FOLLOWING DOESNT WORK:
        <%= render "myapp_web/templates/group_mail/approved_group_member.html", @email_data %>
    """
  end

I was using advice from @LostKobrakai and @Kurisu from this post, but I think that render function only works in a standard Phoenix app where you have access to Conn. I only have access to Socket.

The error I get is: undefined function render/2

How can I pull in the html being used for Swoosh and render it with assigns just for display?

Worst case, I can just copy the html into my LiveView, but that creates a support headache. I risk modifying the HTML file and not updating the liveivew version. Hoping there is a way to embed that HTML into my Liveview render.

Marked As Solved

annad

annad

Ahhhh … I was looking at Phoenix.Controller.render. I didn’t realize there was also a render in Phoenix.View! For anyone else who runs into this issue:

First see solution given by @trisolaran. That fixed the problem.

In reading up more about Phoenix.view.render, I ran across this page on Sharing Views and Templates. I completely missed this because I was looking at Phoenix.Controller.render hexdocs.

Thank you @trisolaran for the solution!

Also Liked

trisolaran

trisolaran

You can use Phoenix.View.render/3 directly for this:

<%= Phoenix.View.render MyAppWeb.GroupMailView, "approved_group_member.html", @email_data %>

You would need to define a MyAppWeb.GroupMailView module that does use MyAppWeb, :view,

LostKobrakai

LostKobrakai

What do you consider a pure html partial? Function component results can be turned into html without Phoenix.View using Phoenix.HTML.Safe — Phoenix.HTML v4.3.0.

LostKobrakai

LostKobrakai

Importing external files has always been (Phoenix.View wraps it) and still is (Phoenix.Component wraps it) the responsibility of Phoenix.Template. Though as the name states it’s meant to import template files more than static files, but you could have a template engine, which just wraps the static content in a function – or you’re just fine with the html being parsed for template parts only for none to be found.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

We're in Beta

About us Mission Statement