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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement