Stillat / blade-parser

A library that makes it simple to parse, analyze, and manipulate Blade templates. Designed with tool makers in mind ❤️
https://bladeparser.com/
MIT License
125 stars 3 forks source link
laravel parser php

Blade Parser is library for Laravel that makes it easy to parse, analyze, and manipulate Blade templates.

The library is composed of many major components:

Simple to Use

Parsing Blade templates is incredibly simple using the Documents API. As an example, this is all that is needed to parse a template:

<?php

use Stillat\BladeParser\Document\Document;

$template = <<<'BLADE'
    Hello, {{ $world }}
BLADE;

$document = Document::fromText($template);

The Document class provides a powerful abstraction, making it simple to quickly retrieve information about a template.

For instance, if we wanted to extract all the components from our template we could do this:

<?php

// Do something with all component tags in the template.
$document->getComponents()
          ->each(fn($node) => ...);

If we were only interested in a component named alert, we could instead use:

<?php

// Find all "alert" components.
$document->findComponentsByTagName('alert')
         ->each(fn($node) => ...);

These examples hardly scratch the surface, and you are encouraged to read through the Documentation.

Built-in Validation Command

This library also ships with a configurable blade:validate Artisan command which can be used to validate all Blade templates within a project.

To configure the command, you will need to publish its configuration files using the following command:

php artisan vendor:publish --tag=blade

To run the validation against your project, you can issue the following Artisan command:

php artisan blade:validate

If any validation issues were detected they will be displayed in your terminal.

There are many configuration options available, and if you'd like to learn more you can find them documented in the Configuring the Validate Command article.

License

This parser library is open-sourced software licensed under the MIT license.