DEV Community

Benjamin Delespierre
Benjamin Delespierre

Posted on

How to check Laravel view syntax

See it on GitHub

TL;DR

From inside your Laravel project, do:

composer require bdelespierre/laravel-blade-linter
php artisan blade:lint
Enter fullscreen mode Exit fullscreen mode

Example output:

PHP Parse error:  syntax error, unexpected ')' in resources/views/_form_errors.blade.php on line 19
Enter fullscreen mode Exit fullscreen mode

Add it to your pre-commit hook so you never commit an invalid file

  1. Open .git/hooks/pre-commit

  2. Add the following:

#!/bin/bash
output=$(git diff --name-only --diff-filter=d --cached | grep ".blade.php$" | xargs -r php artisan blade:lint)
exitcode=$?

C_RESET='\e[0m'
C_RED='\e[31m'
C_GREEN='\e[32m'

if [[ 0 == $exitcode || 130 == $exitcode ]]; then
    echo -e "${C_GREEN}BLADE OK!${C_RESET}"
else
    echo -e "${C_RED}BLADE NOK!${C_RESET}\n\n$output"
    exit 1
fi
Enter fullscreen mode Exit fullscreen mode
  1. ???
  2. PROFIT!!!

As usual, leave a like and a comment. Don't hesitate to reach out if you have issues with this script.

Top comments (0)