Code Monkey home page Code Monkey logo

prettier-plugin-blade's Introduction

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)

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
Screen.record.from.2022-01-27.20.53.05@2x_0.5.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.

key description
--tab-width Number of spaces per indentation level. default: 4
--print-width The line length where Prettier will try wrap. default: 120
--wrap-attributes The way to wrap attributes. [auto|force|force-aligned|force-expand-multiline|aligned-multiple|preserve|preserve-aligned]. default: auto
--end-with-new-line End output with newline. default: true

.prettierrc example

{
  "printWidth": 120,
  "tabWidth": 4,
  "wrapAttributes": "auto"
}

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

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
  • Multiline expression

blade-formatter is line based formatter so multiline expression often causes unexpected behaviour.

❌ Example of unexpected code

<div>
    @php
    $myvar = "lorem
       ipsum";
    @endphp
</div>

⭕ Example of expected code

<div>
    @php
    $myvar = "lorem\nipsum";
    @endphp
</div>

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>

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

LICENSE

MIT

prettier-plugin-blade's People

Contributors

shufo avatar github-actions[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.