goodtouch

goodtouch

SmartIndentationEngine — Clean indentation for EEx templates

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?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
385 14863 120
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
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
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
zachdaniel
Introducing AshStorage! Attachment and file management that slots directly into your resources :smiling_face_with_sunglasses: I had hope...
New

Other Trending Topics Top

type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
yureehuh
Introduction Founded in 2017 by landscape ecologist and fire mitigation expert Harry Statter, Frontline developed the first fully integra...
New

We're in Beta

About us Mission Statement