Skip to content
petecoopPublic

About

A formatter for Laravel Blade templates

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

bladefmt

A formatter for Laravel Blade templates that combines other formatting tools: Oxc & Mago to handle the things they are best at.

Features

  • Extremely fast Blade template formatting (over 50x faster than other Blade formatters, using about a tenth of the memory, see benchmarks)
  • html, js, css are formatted by Oxfmt
  • PHP is formatted by Mago
  • Livewire & Alpine.js expression formatting
  • Tailwind class sorting
  • Formatting failures have nice error messages and works well as a basic linter for blade

Usage

Run bladefmt to format all .blade.php files in the current directory and its subdirectories. Pass files, directories, or quoted globs to narrow the selection. Files are updated in place by default.

bladefmt
bladefmt resources/views
bladefmt 'resources/**/*.blade.php' '!**/fixtures/*.blade.php'
bladefmt --check resources/views
bladefmt --stdin-filepath view.blade.php < view.blade.php

--check lists files that would change without writing.

--stdin-filepath writes formatted output to stdout.

Installation

macOS and Linux:

curl --proto '=https' --tlsv1.2 -LsSf https://github.com/petecoop/bladefmt/releases/latest/download/bladefmt-installer.sh | sh

Windows:

powershell -ExecutionPolicy Bypass -c "irm https://github.com/petecoop/bladefmt/releases/latest/download/bladefmt-installer.ps1 | iex"

The install script is generated by dist for each release. It downloads the prebuilt binary for your platform from the latest GitHub release, verifies its checksum, and moves it to ~/.local/bin (or $XDG_BIN_HOME when set). If that directory isn't on your PATH, the script adds it through your shell profile (~/.zshrc, ~/.bashrc, ~/.profile, or fish config). On Windows, the script adds it to your user PATH instead. Set BLADEFMT_NO_MODIFY_PATH=1 to skip that step.

Update

Update to the latest release, or a specific one, with:

bladefmt --self-update
bladefmt --self-update 1.0.0

Laravel Boost

In a Laravel project that uses Laravel Boost, add bladefmt instructions for AI agents to .ai/guidelines/bladefmt.md:

bladefmt --boost
php artisan boost:update

Run bladefmt --boost again after updating bladefmt to refresh the guideline.

Configuration

Bladefmt reads matching .editorconfig rules for each template. These are the defaults:

[*.blade.php]
indent_style = space
indent_size = 4
max_line_length = 100
end_of_line = lf
insert_final_newline = true

PHP

Bladefmt reads nearest mago.toml.

php-version = "8.4"

[formatter]
single-quote = true
trailing-comma = true

See Mago Configuration Reference for a full list of values.

JS

Bladefmt reads the nearest .oxfmtrc.json or .oxfmtrc.jsonc in the template's parent directories.

{
  "singleQuote": false,
  "semi": true,
  "trailingComma": "all",
}

See Oxfmt Configuration Reference for a full list of values.

Ignoring code from formatting

Ignore next line

{{-- bladefmt-ignore --}}
<td   align="right">{{ $total }}</td>

Ignore block:

{{-- bladefmt-ignore-start --}}
@if ($table)
    <td   align="right">{{ $total }}</td>
@endif
{{-- bladefmt-ignore-end --}}

Ignore the whole file:

{{-- bladefmt-ignore-file --}}

Also supported for compatibility: oxfmt-ignore, prettier-ignore, prettier-ignore-start/-end, and blade-formatter's blade-formatter-disable-next-line, blade-formatter-disable/-enable.

VS Code Auto Formatting

Install Custom Local Formatters and make sure bladefmt is on your PATH. Add this to your VS Code settings.json (merge with any existing formatter settings):

{
  "customLocalFormatters.formatters": [
    {
      "command": "bladefmt --stdin-filepath \"${file}\"",
      "languages": ["blade"],
      "patterns": ["**/*.blade.php"]
    }
  ],
  "[blade]": {
    "editor.defaultFormatter": "jkillian.custom-local-formatters",
    "editor.formatOnSave": true
  }
}

Benchmarks

Checking the formatting of 436 Blade templates (1.9 MB) from Coolify, Relaticle and Laravel Pulse on an Apple M3 Max with 14 cores. Every tool runs in check mode, so each one formats every template without writing any files.

Tool Time Peak memory
bladefmt (14 threads) 35 ms 36 MB
bladefmt (1 thread) 191 ms (×5.5) 22 MB (×0.6)
prettier-plugin-laravel-blade 1.6.0 1.87 s (×54) 378 MB (×10.5)
prettier-plugin-blade 3.3.3 2.34 s (×67) 356 MB (×9.9)
@shufo/prettier-plugin-blade 1.16.3 6.38 s (×184) 532 MB (×15)

Acknowledgements

The test suite originates from shufo/blade-formatter

License

MIT License. Licenses for bundled dependencies are listed in THIRD_PARTY_LICENSES.md.

About

A formatter for Laravel Blade templates

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages