Search
Blade Parser

Blade Parser

Compiling Documents

We can compile a document's echo statements, directives, and components using the compile method. Internally, the document will create a compiler instance by invoking the CompilerFactory::makeCompiler() factory method for us.

By default, the compiler will use all the default configuration values when compiling our document:

1<?php
2 
3use Stillat\BladeParser\Document\Document;
4 
5$template = <<<'BLADE'
6 {{ $hello {{ $world }}
7BLADE;
8 
9$doc = Document::fromText($template);
10 
11$compiled = $doc->compile();

Our example would produce the following compiled output:

1{{ $hello <?php echo e($world); ?>

#Modifying Compiler Behavior

However, there are times when it may be beneficial to change the behavior of the compiler. To do this, we can supply an instance of the Stillat\BladeParser\Document\DocumentCompilerOptions class, which will then be used internally to modify the compiler's behavior. For example, we can change the compilation behavior to instead throw an error on our previous template:

1<?php
2 
3use Stillat\BladeParser\Document\Document;
4use Stillat\BladeParser\Document\DocumentCompilerOptions;
5 
6$template = <<<'BLADE'
7 {{ $hello {{ $world }}
8BLADE;
9 
10$doc = Document::fromText($template);
11 
12$compiled = $doc->compile(new DocumentCompilerOptions(
13 failOnParserErrors: true
14));

Now, instead of producing any compiled output an instance of Stillat\BladeParser\Errors\Exceptions\CompilationException will be thrown with the following message:

1[BLADE_P001002] Unexpected {{ while parsing echo on line 1

#Available Methods

#compile

Compiles the current document.

Compiles the current document to PHP. The compiler instance used internally is constructed by calling the CompilerFactory::makeCompiler() static method.

1<?php
2 
3use ?Stillat\BladeParser\Document\DocumentCompilerOptions;
4 
5public function compile(
6 DocumentCompilerOptions $options = null
7): string;

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.