cosmicrose

cosmicrose

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.

Showing Posts 1 to 6

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 OP

I’d prefer not to involve Phoenix just for this, and I’m trying to solve for the proliferation of tiny templates like those in the first place. It can get really out-of-hand and I’m looking for an alternative.

sodapopcan

sodapopcan

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

al2o3cr

al2o3cr

Oops, I typed my earlier response too fast without checking the version of the docs I was referencing - the EEx sigil used to be in Phoenix.HTML but was deprecated.

You could reuse the implementation, though. Apart from the Phoenix-specific engine, there’s no other coupling to that package:

cosmicrose

cosmicrose OP

That does sweeten things up a little!

cosmicrose

cosmicrose OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to &lt;%= web_namespace %&gt;.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews