Blade Indenter

GitHub license Maintenance GitHub release (latest by date)

A very simple formatter for Laravel 5.8+ Blade templates


This package just indents blade template lines following very simple rules.
It expects a valid and well formed code and won't format or validate code nor deal well with instructions on multiple lines.
It was mainly designed to format generated files in my CRUD generator for Laravel.

Any contribution or feedback is highly welcomed, please feel free to create a pull request or submit a new issue.

Example

Input:
@extends('layout')

@section('title', $title)

@section('content')
<h1>
@if($article->exists)
Edit Article {{ $article->id }}
@php($url = route('articles.update', $article->id))
@else
Create a new Article
@php($url = route('articles.store'))
@endif
</h1>

{!! Form::model($article, ['url' => $url]) !!}

<div id='title-group'>
{!! Form::label('title', 'Title') !!}
{!! Form::text('title') !!}
@error('title')
<p>{{ $message }}</p>
@enderror
</div>

{!! Form::submit('Save') !!}

{!! Form::close() !!}
@endsection
Output:
@extends('layout')

@section('title', $title)

@section('content')
    <h1>
        @if($article->exists)
            Edit Article {{ $article->id }}
            @php($url = route('articles.update', $article->id))
        @else
            Create a new Article
            @php($url = route('articles.store'))
        @endif
    </h1>

    {!! Form::model($article, ['url' => $url]) !!}

    <div id='title-group'>
        {!! Form::label('title', 'Title') !!}
        {!! Form::text('title') !!}
        @error('title')
            <p>{{ $message }}</p>
        @enderror
    </div>

    {!! Form::submit('Save') !!}

    {!! Form::close() !!}
@endsection

Quick start

Simply import the package with composer:

composer require bgaze/laravel-blade-indenter

Configuration can be published to /config/blade-indenter.php:

php artisan vendor:publish --tag=blade-indenter-config

The package exposes a single service which indents Blade string :

$indentedString = resolve(\Bgaze\BladeIndenter\BladeIndenter::class)->indent($stringToIndent);

Two helpers are also provided for convenience :

// Indent a string
$indentedString = indent_blade_string($stringToIndent);

// Indent a blade file, overwrite it and return formatted content.
$indentedFileContent = indent_blade_file($filePath);

// Indent a blade file and return formatted content without overwriting.
$indentedFileContent = indent_blade_file($filePath, false);

Configuration

The indenter supports Blade directives described in official documentation.
However, if needed, you can customize supported tags and directives (check package configuration for defaults).

Two way to do that:

  • publish and edit package configuration.
  • configure BladeIndenter from the boot section of a Service provider.

As an exemple, here is the way to configure indenter for bgaze/bootstrap-form custom directives.

// From boot method of App\Providers\AppServiceProvider:

resolve(\Bgaze\BladeIndenter\BladeIndenter::class)
    // @close directive closes @open, @vertical, @horizontal and @inline directives
    ->addClosingDirectives([
        'open' => 'close',
        'vertical' => 'close',
        'horizontal' => 'close',
        'inline' => 'close',
    ])
    // Indent level won't change on line after one of these directives
    ->addSelfClosingDirectives([
        'text', 'email', 'url', 'tel', 'number', 'date', 'time', 'textarea',
        'password', 'file', 'hidden', 'select', 'range', 'checkbox', 'checkboxes',
        'radio', 'radios', 'label', 'submit', 'reset', 'button', 'link',
    ]);

Self-closing HTML tags

Indentation level won't increase after these HTML tags.
Edit self_closing_tags section in configuration or use following methods:

/**
* @param  array  $tags
* @return BladeIndenter
*/
public function setSelfClosingTags(array $tags);

/**
* @param  array  $tags
* @return BladeIndenter
*/
public function addSelfClosingTags(array $tags);

Self-closing Blade directives

Indentation level won't increase after these directives.
Edit self_closing_directives section in configuration or use following methods:

/**
* @param  array  $directives
* @return BladeIndenter
*/
public function setSelfClosingDirectives(array $directives);

/**
* @param  array  $directives
* @return BladeIndenter
*/
public function addSelfClosingDirectives(array $directives);

Closing Blade directives

The "end" version of any directive is supported (for instance @endsection for @section), but some directives can also be closed by other directives, like @show closes @section.

Edit closing_directives section in configuration or use following methods to define the mapping of these behaviours (without @ character).

/**
* @param  array  $directives
* @return BladeIndenter
*/
public function setClosingDirectives(array $directives);

/**
* @param  array  $directives
* @return BladeIndenter
*/
public function addClosingDirectives(array $directives);

Examples:

'closing_directives' => [
    'section' => 'show',
    'opening_directive_2' => ['closing_directive_21', 'closing_directive_22'],
],

Else Blade directives

Else directive will be indented one level down, but previous level will be preserved on following line.
Edit else_directives section in configuration or use following methods:

/**
* @param  array  $directives
* @return BladeIndenter
*/
public function setElseDirectives(array $directives);

/**
* @param  array  $directives
* @return BladeIndenter
*/
public function addElseDirectives(array $directives);

Packages

Feel free to visit my other packages:

Bootstrap 4 forms builder for Laravel 5.8+

This package uses in background Laravel Collective HTML to simplify Bootstrap 4 forms creation into Laravel applications.
Model form binding and automatic error display are supported, as well as most of Bootstrap forms features : form layouts, custom fields, input groups, ...

Github Documentation

PHP-CS-Fixer for Laravel 5.5+

This package allows to use PHP-CS-Fixer right into Laravel 5.5+ applications to format PHP code.

Github Documentation

Bootstrap 4 dialogs

BSD is a tiny and flexible collection of dialog popups based on Bootstrap 4 modals.
Custom dialogs can be easily defined, in addition to built-in ones (alert, confirm and prompt).

Github Documentation

Bootstrap Color Palette

BCP is a simple color palette for Bootstrap 4, like in Google doc, built on the top of Bootstrap 4 Popover plugin.

Github Documentation

Laravel Kvstore

A simple and easy to use key-value database store for Laravel 5.5+
All values are stored into database and managed using cache to avoid unecessary queries.
Casting is supported to manage values type.

Github Documentation

Php DotEnv

A simple and standalone DotEnv parser for PHP 5.6+

Github Documentation