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: Moved to Codeberg · GitHub. 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!

Last Post!

cosmicrose

cosmicrose

Putting all these little templates into their own functions using a sigil does not solve the problem of having too many templates.

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
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
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
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
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

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement