Yeah, I'm sure you've heard about Laravel Blade Templates. On the surface it's pretty much the same, but it's realized in a much better way and has several useful functionality.
\Greg\View\BladeCompiler is an independent compiler which you can work with.
It has only independent directives, but you can extend it any time.
Implements: \Greg\View\CompilerStrategy.
Example:
$compiler = new \Greg\View\BladeCompiler(__DIR__ . '/compiled');
$compiledFile = $compiler->getCompiledFile(__DIR__ . '/welcome.blade.php');
include $compiledFile;- View Blade Compiler - An extended Blade Compiler, specially for the Viewer Contract.
Initialize the compiler.
__construct(string $compilationPath)$compilationPath - Compiled files path;
Example:
$compiler = new \Greg\View\BladeCompiler(__DIR__ . '/compiled');Includes Compiler Strategy methods.
- compileFile - Compile a template file;
- compileString - Compile a template string;
- addCompiler - Add a compiler;
- addDirective - Add a template directive;
- addEmptyDirective - Add an empty template directive;
- addOptionalDirective - Add a template directive with optional parameters.
Compile a template file.
compileFile(string $file): stringCompile a template string.
compileString(string $string): stringAdd a template compiler.
addCompiler(callable(string $content): string $compiler): $this$compiler - A callable compiler.
$content - Content to compile.
Add a template directive.
addDirective(string $name, callable(string $args): string $compiler): $this$name - Directive name;
$compiler - Directive callable.
$args - Directive arguments.
Add an empty template directive. See addDirective method.
Add a template directive with optional parameters. See addDirective method.
- Display data
- Statements
- Directives
- Stop - Stop template execution.
Display data throw htmlentities to prevent XSS attacks.
Example:
Hello, {{ $name or 'guest' }}.Display data as it is.
Example:
Hello, {!! $name or '<em>guest</em>' !!}.Writing template comments.
Example:
{{-- This comment will not be present in the rendered HTML --}}Example:
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endifExample:
@unless (Auth::check())
You are not signed in.
@elseunless (Auth::isVerified())
You should verity your account.
@else
Hello, {{ Auth::name() }}
@endunlessExample:
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endforExample:
@foreach ($users as $user)
@continue($user->type == 1)
<p>This is user {{ $user->id }}</p>
@break($user->number == 5)
@empty
<p>No users</p>
@endforeachYou can also set a loop variable in foreach. This variable provides access to some useful bits of information such as the current loop index and whether this is the first or last iteration through the loop.
Example:
@foreach ($users as $user, $loop)
@if ($loop->first)
This is the first iteration.
@endif
@if ($loop->last)
This is the last iteration.
@endif
<p>This is user {{ $user->id }}</p>
@endforeachThe loop variable contains a variety of useful properties:
$loop->index- The index of the current loop iteration (starts at 0);$loop->iteration- The current loop iteration (starts at 1);$loop->remaining- The iteration remaining in the loop;$loop->count- The total number of items in the array being iterated;$loop->first- Whether this is the first iteration through the loop;$loop->last- Whether this is the last iteration through the loop;$loop->depth- The nesting level of the current loop;$loop->parent- When in a nested loop, the parent's loop variable.
Example:
@while (true)
<p>I'm looping forever.</p>
@endwhileExample:
@switch ($color)
@case('red')
The color is red.
@break
@case('green')
The color is green.
@break
@default
The color is blue.
@endswitchIf you don't want to compile some content, you can use @verbatim statement.
Example:
@verbatim
<div class="container">
Hello, {{ name }}.
</div>
@endverbatimStop executing the template.
Example:
I will be visible.
@stop
I will not be visible.