goodtouch

goodtouch

Over the last few days I built a small custom EEx engine that tackles a frustration I’ve had for a while trying to get proper indentation with EEx templates. I thought some of you might find it useful too.

I’ve been doing a lot of Codegen lately and when writing EEx templates with loops and conditionals, I often end up facing a choice between:

  • Messy template code to get properly formatted output
  • Clean template code but with weird (or even invalid) spacing in the output

This library attempts to fix this by extending EEx.SmartEngine with a special <%| tag that injects some context-aware string manipulation into the AST to ensure that the output is indented in a way that makes sense (at least to me :sweat_smile:).

It also provides a ~TT sigil for convenience and supports including (and re-indenting) “partial” templates.

Quick Example

defmodule Template.DockerCompose do
  import SmartIndentationEngine.Template

  def render(assigns) do
    ~TT"""
    services:
      <%| for {name, config} <- @services do %>
        <%= name %>:
          <%= include :service, config: config %>
      <% end %>
    """
  end
  
  def service(assigns) do
    ~TT"""
    image: <%= @config.image %>
    ports:
      <%| for port <- @config.ports do %>
        - <%= port %>
      <% end %>
    # ...
    """
  end
end

Template.DockerCompose.render(
  services: %{
    livebook: %{
      image: "ghcr.io/livebook-dev/livebook:0.15.5",
      ports: [8080, 8081]
    }
  }
)

This will produce the following output:

services:
  livebook:
    image: ghcr.io/livebook-dev/livebook:0.15.5
    ports:
      - 8080
      - 8081
    # ...

Give It a Try!

I just published it on Hex: smart_indentation_engine.

https://github.com/goodtouch/smart_indentation_engine

If you’re working with EEx templates, I’d love for you to try it out. Let me know what you think, if you encounter any issues, edge cases, or if you have ideas for improvements!

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews