cosmicrose

cosmicrose

Implementing template fragments for EEx

I’m trying to implement template fragments in EEx. My extremely rough draft lets you create a template file that looks like this:

<%| first_fragment do %>
I'm the first fragment!
<% end %>
<%| second_fragment do %>
I'm the second fragment!
<% end %>

Evaluating this template with the arguments [fragment: :first_fragment] (and a custom EEx engine) would result in the string "I'm the first fragment!". Evaluating the template with no fragment argument would yield both sentences. Notice that this is using the reserved <%| marker for an EEx expression, I’m just using that because it gives my EEx.Engine implementation an easy flag to evaluate the upcoming AST differently. The Engine implementation just replaces this expression with an if statement that only returns the text inside the block if the fragment variable matches the given token.

I haven’t worked with macros or with EEx internals before, so I am looking for feedback and recommendations on how I can do this better, perhaps more idiomatically. Using EEx’s <%| expression marker as a flag to parse the next word as the template name feels extremely hacky but I wasn’t able to get this working any other way.

The code is available here: GitHub - Cantido/stencil: Template fragments in EEx. I’m open to any and all feedback.

As a footnote, I would like to explain why I want this functionality for EEx, when I can just decompose one template into several smaller templates. It would certainly be easier to re-use templates that way. I read an explanation on the HTMX website about template fragments (here: </> htmx ~ Template Fragments) and I think they may be very handy to avoid a proliferation of small template files for HTML fragments that are only used in one place. The fragment stays surrounded by its context, but can be extracted and returned on its own. This is handy if you’re serving a lot of smaller HTML fragments like you do when you use HTMX. It’s a rare feature in templating languages, but I would love to see if it can be implemented in EEx or in a separate library.

Most Liked

sodapopcan

sodapopcan

Phoenix.HTML is its own package with a single optional dependency on Plug in case that makes any difference.

al2o3cr

al2o3cr

If you’re using Phoenix, IMO the EEx sigil + functions would be more idiomatic:

defmodule SomeModuleWithFragments do
  use Phoenix.Html

  def first_fragment do
    ~E"""
    I'm the first fragment!
     """
  end

  def second_fragment do
    ~E"""
    I'm the second fragment!
     """
  end
end
cosmicrose

cosmicrose

That does sweeten things up a little!

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
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

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
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
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement