Skip to main content

ERB Formatter & Beautifier

Re-indent Rails .html.erb templates, right in your browser.

ERB Input

Up to 1,000,000 bytes (1 MB); live formatting up to 100,000 bytes. Current size: 0 bytes.

Options

Indentation
Advanced

Formatted ERB

Paste an ERB template or load an example.

Formats ERB and HTML only. Ruby code is never executed, and script, style, pre and textarea content is kept as written.

How to use

  1. 1.Paste an ERB template into the input or load an example.
  2. 2.Templates up to 100,000 bytes are formatted as you type; for larger ones select Format or press Ctrl+Enter (⌘+Enter on Mac).
  3. 3.Choose the indentation and whether to normalize tag spacing or put block tags on their own lines.
  4. 4.Review any diagnostics, then copy the result or download it as an .html.erb or .erb file.
  5. 5.Reset clears the template and restores the default options.

ERB Formatter & Beautifier

This tool re-indents ERB (Embedded Ruby) templates such as Rails .html.erb views. It reads the template with its own ERB and HTML tokenizer, so it understands <% %>, <%= %>, <%== %>, <%- %> and <%# %> tags, the <%% literal and the -%> trim marker. It only changes layout: each line gets the indentation of its HTML and Ruby nesting, trailing spaces are removed, and, if you allow it, the single space inside the delimiters is normalized. Ruby code is never executed or rewritten, and text inside a line is never reflowed. Formatting the output again returns it unchanged.

How indentation works

Indentation combines HTML element depth and Ruby block depth. Ruby blocks open with if, unless, case, while, until, for, begin, def, class or module at the start of a statement, with do or do |args|, or with a curly-brace block that stays open, and close with end or the closing brace. Lines starting with else, elsif, when, in, rescue or ensure move back one level. Modifier forms such as x if y, method calls such as range.end and hash keys such as class: are not block keywords. When HTML elements and Ruby blocks overlap, for example a div opened in both branches of an if, those lines keep their original relative indentation and a warning explains why.

ERB tags and %> inside strings

ERB ends a tag at the first %>, even when it appears inside a Ruby string literal. Erubi, the ERB engine in Rails, scans the template the same way, and so does this formatter. A tag cut short like this is kept as written and flagged so you can split the string, for example "%" + ">". Content of script, style, pre and textarea elements is kept byte-for-byte, including its indentation, because whitespace there can change behavior.

Privacy

Formatting runs entirely in your browser. Your template is never uploaded, executed or stored; only the formatter options are remembered on this device.

Frequently asked questions
Does the ERB formatter run my Ruby code?
No. It only reads the template text in your browser. Ruby code inside <% %> tags is never executed or rewritten; only the indentation of lines and, optionally, the single space inside the delimiters change.
Why are my script, style and pre blocks not re-indented?
Content of script, style, pre and textarea elements is kept byte-for-byte, including its indentation, because whitespace there can change what the page does or shows.
What happens when a %> appears inside a Ruby string?
ERB ends a tag at the first %>, even inside a string, just like Erubi in Rails. The formatter does the same, keeps that tag as written and warns you so you can split the string.
Can it minify ERB templates?
No. This tool only formats. Removing whitespace from ERB templates can change the rendered HTML, so no minify mode is offered.

CodingTool is officially live on