LogoTemplate0
Logo of Slime on template0.com

Slime

Minimalistic HTML templates for Elixir, inspired by Slim.

Introduction

Slime is an Elixir template engine that renders Slim-like indentation-based markup into HTML, letting Phoenix developers write templates with far less closing-tag boilerplate.

What is Slime?

Slime is an open-source, MIT-licensed Elixir library that converts Slim-style template source into HTML strings. It takes a template as input (a string containing indentation-sensitive markup and embedded Elixir expressions) and produces HTML output via the Slime.render/2 function, which accepts a keyword list of variables bound in the template. Slime is developed by the slime-lang GitHub community and is designed to slot into Phoenix applications through the companion PhoenixSlime project.

What makes Slime stand out?

Slime stands out by translating Ruby Slim's concise markup to Elixir's expression model, while adding features like embedded engines, doctype shortcuts, and precompilation that EEx doesn't provide out of the box. Here are the concrete capabilities:

  • Slim-compatible syntax — Write markup without closing tags; use .class and #id shortcuts, attributes on a single line, and indentation hierarchy.
  • Direct Elixir interpolation — Evaluate expressions with - (side effect) and = (insert into output); interpolate variables inside text as #{var} and inside attribute values.
  • Embedded engines — Run JavaScript, CSS, Elixir, and EEx blocks inside templates via javascript:, css:, elixir:, and eex:; define custom engines in config :slime, :embedded_engines.
  • Doctype shortcuts — Emit common doctypes with one word: doctype html, doctype xml, doctype transitional, doctype strict, doctype frameset, doctype 1.1, doctype basic, and doctype mobile.
  • Compile-time precompilation — Use Slime.function_from_file/5 and Slime.function_from_string/5 to compile templates into module functions, matching EEx workflow.
  • Boolean attribute handling — Set checked=true to render the attribute or checked=false to omit it entirely.
  • Debugging aid — Add config :slime, :keep_lines, true to keep original template line numbers in output EEx and HTML for easier exception tracing.
Who should use Slime?

Slime is a fit for Elixir and Phoenix developers who want a more concise, indentation-based HTML templating style. Specifically:

  • Phoenix developers who want a more readable, shorter alternative to EEx templates for views and HTML emails.
  • Elixir developers building generated HTML — e.g., server-rendered pages, HTML reports, or static site output — who prefer a Ruby Slim–like syntax.
  • Teams migrating from Ruby Slim who need the same indentation-based templating style inside an Elixir/Phoenix stack.
What can you do with Slime?

With Slime you can render Slim-style templates both at runtime and at compile time, and extend it with custom embedded engines. Concrete uses include:

  • Phoenix view templates: Render Slim-style .slime files in Phoenix via PhoenixSlime, with access to the view's assigns.
  • Programmatic HTML generation: Call Slime.render/2 directly on template strings, passing assign values such as site_title: "Website Title" to bind variables.
  • Embedded custom engines: Build a Markdown engine (e.g., using Earmark) and attach it through the application config to render markdown: blocks.
How does Slime work?

Templates are written in Slim-inspired indentation syntax. At runtime you call Slime.render(source, assigns) to render a string; at compile time you can use Slime.function_from_string/5 or Slime.function_from_file/5 to generate module functions that return the HTML. Slime's embedded engines are read at compile time, so new engines require recompiling the dependency with mix deps.compile slime --force.

Differences to Ruby Slim

Slime aims for feature parity with the original Slim but follows Elixir idioms. Where Ruby Slim writes - if condition, Slime requires Elixir's do/end form plus an initial = because the conditional's return value is inserted into the template: = if condition do ... - else .... This makes conditionals and loops behave as pure Elixir expressions.

FAQ
Is Slime free?

Yes. Slime is released under the MIT license, so it is free to use, modify, and distribute in commercial and personal projects.

Does Slime work with Phoenix?

Yes, through the companion project PhoenixSlime, which connects Slime templates to Phoenix's view rendering pipeline, letting you use .slime files as your Phoenix view templates.

How do I add a custom embedded engine?

Configure config :slime, :embedded_engines with a map of engine names to modules, then implement the Slime.Parser.EmbeddedEngine behaviour with a render/2 callback. Because engines are read at compile time, run mix deps.compile slime --force after changing the config.

How do I debug errors in Slime templates?

Set config :slime, :keep_lines, true in your config.exs to preserve original template line numbers in the generated EEx/HTML output. Be aware the output differs slightly: | behaves like ', and empty lines are not ignored.

screenshot of Slime on template0.com

Information

GitHub Info

  • Stars371
  • Last maintained2024/06/08
  • LicenseMIT
  • Primary languageElixir

Categories

Tags

Newsletter

Join the Community

Subscribe to our newsletter for the latest news and updates