Search
Blade Parser

Blade Parser

Compilation Targets

The compiler implementation supports the concept of "compilation targets." Compilation targets instruct the compiler to change its behavior depending on where the resulting PHP code is used. Currently, the following compilation targets are supported:

  • Template Output: Instructs the compiler to emit PHP code that will appear as part of the main compiled template body

  • Component Parameter: Instructs the compiler to emit PHP code that will appear inside component tag parameters

#Third-Party Compilation Target Support

To maintain compatibility with existing behavior surrounding directive compilation, compilation targets are not exposed to third-party libraries. This document serves as a reference for those wishing to contribute to the compiler implementation provided by this library.

#Example Use

An example of compilation targets being used internally is the compilation of echo statements. The following examples will compile the same Blade template but with different compilation targets:

1<?php
2 
3use Stillat\BladeParser\Compiler\CompilerFactory;
4use Stillat\BladeParser\Compiler\CompilationTarget;
5 
6$compiler = CompilerFactory::makeCompiler();
7 
8$template = <<<'BLADE'
9{{ $hello }}
10BLADE;

If we set the compiler to the TemplateOutput target:

1<?php
2 
3$compiler->setCompilationTarget(CompilationTarget::TemplateOutput);
4$compiler->compileString($template);

the compiler emits the following output:

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

Changing the compilation target to ComponentParameter dramatically changes the final result:

1<?php
2 
3$compiler->setCompilationTarget(CompilationTarget::ComponentParameter);
4$compiler->compileString($template)

which produces the following output:

1'.e($hello).'

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.