New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More ā†’
Socket
Sign inDemoInstall
Socket

@shufo/prettier-plugin-blade

Package Overview
Dependencies
Maintainers
1
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@shufo/prettier-plugin-blade

- Automatically Indents markup inside directives - Automatically add spacing to blade templating markers - PHP 8 syntax support (null safe operator, named arguments) - Compliant to PSR-2 coding standard (PHP code inside directives) - Automatical

  • 1.8.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15K
decreased by-0.4%
Maintainers
1
Weekly downloads
Ā 
Created
Source
Prettier PHP

Prettier Blade Plugin

GitHub Workflow Status npm version

Format your blade template using Prettier

Features

  • Automatically Indents markup inside directives
  • Automatically add spacing to blade templating 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

Usage (CLI)

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

Troubleshooting

If you get something error like can not find plugins, update your .prettierrc.json file to specify parser:

{
    "overrides": [
        {
            "files": ["*.blade.php"],
            "options": {
                "tabWidth": 4,
                "parser": "blade"
            }
        }
    ]
}

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
--end-with-new-lineEnd output with newline. default: true
--sort-tailwindcss-classesSort Tailwind CSS classes. It will automatically lookup and respects tailwind.config.js if exists. default: false
--tailwindcss-config-pathPath to custom Tailwind config. This option is available only when --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

.prettierrc example

{
    "printWidth": 120,
    "tabWidth": 4,
    "wrapAttributes": "auto",
    "sortTailwindcssClasses": true,
    "sortHtmlAttributes": "none",
    "noPhpSyntaxCheck": false
}

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 format on a specific line, you can use comment in the following format:

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

or

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

Editor Integration

Below editors are confirmed working with this plugin.

VSCode

You can use Prettier extension for VSCode to format blade within VSCode. You must install this plugin as local dependencies. see https://github.com/prettier/prettier-vscode#prettier-resolution

If you want to use formatter without Prettier, please consider to using vscode-blade-formatter

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 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 error: 'Prettier: File *.php has unsupported type'

Limitation

This plugin is based on blade-formatter that does not generate ASTs with lexer, so it might be 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 make blade template as simple as possible for better formatting.

API

You can format blade text programmatically using prettier

const prettier = require("prettier");

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

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

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

shufo
Shuhei Hayashibara
howdu
Beej
ianjamieson
Ian Jamieson
mortenscheel
Morten Scheel

LICENSE

MIT

Keywords

FAQs

Package last updated on 14 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with āš”ļø by Socket Inc