1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-02 22:58:01 +00:00

Add Format Specific html.erb Template Files as a Rails TIL

This commit is contained in:
jbranchaud
2025-10-16 08:55:51 -05:00
parent ec0e84664f
commit 5b3f1536fd
2 changed files with 41 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1660 TILs and counting..._
_1661 TILs and counting..._
See some of the other learning resources I work on:
- [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7)
@@ -1056,6 +1056,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Find Records With Multiple Associated Records](rails/find-records-with-multiple-associated-records.md)
- [Force All Users To Sign Out](rails/force-all-users-to-sign-out.md)
- [Format DateTime With Builtin Formats](rails/format-datetime-with-builtin-formats.md)
- [Format Specific html.erb Template Files](rails/format-specific-html-erb-template-files.md)
- [Generate A Model](rails/generate-a-model.md)
- [Generate A Rails App From The Main Branch](rails/generate-a-rails-app-from-the-main-branch.md)
- [Generating And Executing SQL](rails/generating-and-executing-sql.md)

View File

@@ -0,0 +1,39 @@
# Format Specific html.erb Template Files
There are a few tools out there that can do formatting of `*.html.erb` template
files. One that I like is
[nebulab/erb-formatter](https://github.com/nebulab/erb-formatter#readme)
because it is ready to use in many different editors. That means that it is
easier to adopt on a team of developers with different editor preferences.
That said, there are projects where I don't necessarily want it wired up to run
on save because the formatting changesets will be too agressive. Instead, I
want to run it manually on specific files as I see fit.
To do this, I install the formatter tool:
```bash
$ gem install erb-formatter
```
And now it is available as a CLI tool (try `which erb-format`).
As their docs recommend, I can run it against all files like so:
```
$ erb-format app/views/**/*.html.erb --write
```
If that is too aggressive though, I find it useful to either run against a
specific file:
```
$ erb-format app/views/some/model/index.html.erb --write
```
Or when I'm wrapping up changes on a branch, I like to run it against all the
view files that were touched on this branch:
```
$ git diff --name-only master...HEAD -- app/views | xargs erb-format --write
```