1.14.1 • Published 3 months ago

@shufo/prettier-plugin-blade v1.14.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Format your blade template using Prettier

Features

  • Automatically indent markup inside directives
  • Automatically add spacing to blade template markers
  • PHP 8 syntax support (null safe operator, named arguments)
  • Compliant to PSR-2 coding standard (PHP code inside directives)
  • Automatically sort Tailwind CSS classes with respect of tailwind.config.js

Installation

$ npm install --save-dev @shufo/prettier-plugin-blade prettier

# yarn
$ yarn add -D @shufo/prettier-plugin-blade prettier

# pnpm
$ pnpm add -D @shufo/prettier-plugin-blade prettier

then, add in your Prettier configuration:

{
  "plugins": ["@shufo/prettier-plugin-blade"],
  "overrides": [
    {
      "files": ["*.blade.php"],
      "options": {
        "parser": "blade",
        "tabWidth": 4
      }
    }
  ]
}

Prettier version Compatibilitiy

PrettierPackage
3.x^1.9.x
2.x1.8.x

Usage (CLI)

$ ./node_modules/.bin/prettier --write resources/**/*.blade.php

https://user-images.githubusercontent.com/1641039/151354641-6305805e-8e0c-4226-8331-64195f85160e.mp4

Example

Input

@extends('frontend.layouts.app')
@section('title') foo
@endsection
@section('content')
<section id="content">
<div class="container mod-users-pd-h">
    <div class="pf-user-header">
    <div></div>
    <p>@lang('users.index')</p>
    </div>
        <div class="pf-users-branch">
            <ul class="pf-users-branch__list">
                @foreach($users as $user)
        <li>
            <img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow">
            {{ link_to_route("frontend.users.user.show",$users["name"],$users['_id']) }}
        </li>
        @endforeach
      </ul>
      <div class="pf-users-branch__btn">
      @can('create', App\Models\User::class)
            {!! link_to_route("frontend.users.user.create",__('users.create'),[1,2,3],['class' => 'btn']) !!}
            @endcan
        </div>
  </div>
    </div>
</section>
@endsection
@section('footer')
@stop

Output

@extends('frontend.layouts.app')
@section('title') foo
@endsection
@section('content')
    <section id="content">
        <div class="container mod-users-pd-h">
            <div class="pf-user-header">
                <div></div>
                <p>@lang('users.index')</p>
            </div>
            <div class="pf-users-branch">
                <ul class="pf-users-branch__list">
                    @foreach ($users as $user)
                        <li>
                            <img src="{{ asset('img/frontend/icon/branch-arrow.svg') }}" alt="branch_arrow">
                            {{ link_to_route('frontend.users.user.show', $users['name'], $users['_id']) }}
                        </li>
                    @endforeach
                </ul>
                <div class="pf-users-branch__btn">
                    @can('create', App\Models\User::class)
                        {!! link_to_route('frontend.users.user.create', __('users.create'), [1, 2, 3], ['class' => 'btn']) !!}
                    @endcan
                </div>
            </div>
        </div>
    </section>
@endsection
@section('footer')
@stop

Options

You can use these options for prettier blade plugin in prettier CLI.

keydescription
--tab-widthNumber of spaces per indentation level. default: 4
--print-widthThe line length where Prettier will try wrap. default: 120
--wrap-attributesThe way to wrap attributes. auto|force|force-aligned|force-expand-multiline|aligned-multiple|preserve|preserve-aligned. default: auto
--wrap-attributes-min-attrsMinimum number of html tag attributes for force wrap attribute options. Wrap the first attribute only if 'force-expand-multiline' is specified in wrap attributes. default: 2.
--end-with-new-lineEnd output with newline. default: true
--sort-tailwindcss-classesSort Tailwind CSS classes. It will automatically look for and respect tailwind.config.js if it exists. default: false
--tailwindcss-config-pathPath to your custom Tailwind configuration file. This option is only available if --sort-tailwindcss-classes is present. default: ''
--sort-html-attributesSort HTML Attributes in the specified order. [none | alphabetical | code-guide | idiomatic | vuejs] default: none
--no-php-syntax-checkDisable PHP syntax checking. default: false
--extra-linersComma separated list of tags that should have an extra newline before them. default: head,body,/html
--trailing-comma-phpIf set to false, no trailing commas are printed for php expression. default: true

.prettierrc example

{
  "printWidth": 120,
  "tabWidth": 4,
  "wrapAttributes": "auto",
  "wrapAttributesMinAttrs": 2,
  "sortTailwindcssClasses": true,
  "sortHtmlAttributes": "none",
  "noPhpSyntaxCheck": false,
  "indentInnerHtml": true,
  "extraLiners": "",
  "trailingCommaPHP": true
}

Disabling format in file

To disable formatting in your file, you can use blade/html comments in the following format:

{{-- prettier-ignore-start --}}
    {{ $foo }}
    {{ $bar }}
{{-- prettier-ignore-end --}}

or

<!-- prettier-ignore-start -->
    {{ $foo }}
    {{ $bar }}
<!-- prettier-ignore-end -->

To disable formatting on a specific line, you can use comment in the following format:

{{-- prettier-ignore --}}
    {{ $foo }}

or

<!-- prettier-ignore -->
    {{ $foo }}

Editor Integration

The editors below are confirmed to work with this plugin.

VSCode

You can use Prettier extension for VSCode to format blade in VSCode. You need to install this plugin as a local dependency. see https://github.com/prettier/prettier-vscode#prettier-resolution

If you want to use a formatter without Prettier, please consider to use the vscode-blade-formatter instead.

Vim

You can use coc-prettier plugin on coc.nvim

If you want to use formater without Prettier, please consider to using coc-blade

JetBrains WebStorm, PHPStorm, PyCharm...

You can use the Prettier Plugin for JetBrains IDE.

Add extension setting blade.php to File | Settings | Languages & Frameworks | JavaScript | Prettier | Run for files:

e.g.

{**/*,*}.{js,ts,jsx,tsx,blade.php}

and turn on checkbox On 'Reformat Code' action

Restart your IDE if you get the error: 'Prettier: File *.php has unsupported type'

Limitation

This plugin is based on blade-formatter which does not generate ASTs with lexer, so it might break indentation on complex blade.

Like:

  • The mix of open/closed HTML tag and directives

❌ Example of unexpected code

@if ($user)
    <div>
    @else
    </div>
@endif

⭕ Example of expected code

@if ($user)
    <div>foo</div>
@else
    <div>bar</div>
@endif

Please keep the blade template as simple as possible for better formatting.

API

You can format the blade file programmatically using Prettier's API

// CommonJS
const prettier = require("prettier");

const input = `
<div>
  @if ($user)
  {{ $foo }}
  @else
  {{ $bar }}
  @endif
</div>
`;

const res = await prettier.format(input, { parser: "blade" });
console.log(res);
// =>
//<div>
//    @if ($user)
//        {{ $foo }}
//    @else
//        {{ $bar }}
//    @endif
//</div>

// ES Module
import * as prettier from "prettier";

const input = `
<div>
  @if ($user)
  {{ $foo }}
  @else
  {{ $bar }}
  @endif
</div>
`;
const res = await prettier.format(input, { parser: "blade" });
console.log(res);

Development

$ yarn install
$ yarn run watch # watch changes

Testing

$ yarn install
$ yarn run test

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Contributors

LICENSE

MIT

1.14.1

3 months ago

1.14.0

3 months ago

1.13.5

3 months ago

1.11.1

9 months ago

1.9.1

10 months ago

1.9.0

10 months ago

1.10.1

10 months ago

1.10.0

10 months ago

1.12.0

7 months ago

1.9.2

10 months ago

1.11.0

9 months ago

1.13.2

7 months ago

1.13.1

7 months ago

1.13.0

7 months ago

1.13.4

6 months ago

1.13.3

7 months ago

1.8.13

11 months ago

1.8.11

1 year ago

1.8.12

1 year ago

1.8.9

1 year ago

1.8.10

1 year ago

1.8.8

1 year ago

1.8.7

1 year ago

1.8.2

1 year ago

1.8.6

1 year ago

1.8.5

1 year ago

1.8.4

1 year ago

1.8.3

1 year ago

1.8.1

1 year ago

1.6.3

2 years ago

1.8.0

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.2

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.7

2 years ago

1.5.6

2 years ago

1.5.5

2 years ago

1.4.20

2 years ago

1.4.22

2 years ago

1.4.21

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.13

2 years ago

1.4.12

2 years ago

1.4.15

2 years ago

1.4.14

2 years ago

1.4.17

2 years ago

1.4.16

2 years ago

1.4.19

2 years ago

1.4.18

2 years ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.4.9

2 years ago

1.4.11

2 years ago

1.4.8

2 years ago

1.4.10

2 years ago

1.4.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.3.3

2 years ago

1.2.4

2 years ago

1.3.2

2 years ago

1.2.3

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.2

2 years ago

1.2.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.2.1

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago