maennchen

maennchen

Nest EEX Templates

I’m trying to nest EEX templates. I’d like this approach to be able to pass HTML as a parameter to a render function. To make this happen I tried to use the ~E sigil from Phoenix.HTML.

Given the following code I get an error:

<%= render(AcmeAdmin.PartialView, "_card.html", content: ~E"""
  <table>
      <tr>
        <th><%= "Name" |> gettext %></th>
      </tr>
  </table>
""") %>

** (TokenMissingError) lib/acme_admin/templates/partial/foo.html.eex:4: missing terminator: """ (for heredoc starting at line 1)
    (eex) lib/eex/compiler.ex:45: EEx.Compiler.generate_buffer/4
    (phoenix) lib/phoenix/template.ex:349: Phoenix.Template.compile/3
    (phoenix) lib/phoenix/template.ex:160: anonymous fn/4 in Phoenix.Template."MACRO-__before_compile__"/2
    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
    (phoenix) expanding macro: Phoenix.Template.__before_compile__/1
    lib/acme_admin/views/partial_view.ex:1: AcmeAdmin.PartialView (module)
    (elixir) lib/kernel/parallel_compiler.ex:206: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

I tried to escape the inner eex block with double %%.

How can this be implemented?

Most Liked

LostKobrakai

LostKobrakai

In MyAppWeb.view/0 add:

      defmacro render_block(module, template, assigns, do: block) do
        quote do
          block_content = unquote(block)

          render(
            unquote(module),
            unquote(template),
            unquote(assigns) |> Map.put_new(:block_content, block_content)
          )
        end
      end

and use it like so:

<%= render_block AcmeAdmin.PartialView, "_card.html", assigns do %>
  some HTML
<% end %>
net

net

Doesn’t need to be a macro:

def render_in(module, template, assigns \\ %{}, do: yield),
  do: Phoenix.View.render(module, template, put_in(assigns[:yield], yield))

Last Post!

maennchen

maennchen

Why did I not think of that, this is much a simpler solution than what I imagined. Thanks all!

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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

We're in Beta

About us Mission Statement