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
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 %>
2
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))
2
Last Post!
maennchen
Why did I not think of that, this is much a simpler solution than what I imagined. Thanks all!
0
Popular in Questions
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
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Other popular topics
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
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
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









