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.

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?

Popular in Libraries Top

kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps...
New
praveenperera
FastRSS Parse RSS feeds very quickly: This is rust NIF built using rustler Uses the RSS rust crate to do the actual RSS parsing Speed...
New
New
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. [demo] The distributed characteristics of Elixir and the low memory foo...
New
Crowdhailer
Experimenting with this code. OK.try do user &lt;- fetch_user(1) cart &lt;- fetch_cart(1) order = checkout(cart, user) save_or...
New
mtrudel
Bandit is an HTTP server for Plug and WebSock apps. Bandit is written entirely in Elixir and is built atop Thousand Island. It can serve...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
ostinelli
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New

Other popular topics Top

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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

Sub Categories:

We're in Beta

About us Mission Statement