Skip to content

Latest commit

 

History

History
294 lines (201 loc) · 6.04 KB

BladeCompiler.md

File metadata and controls

294 lines (201 loc) · 6.04 KB

Blade Compiler Documentation

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;

Available extenders:

Table of contents:

Magic methods:

__construct

Initialize the compiler.

__construct(string $compilationPath)

$compilationPath - Compiled files path;

Example:

$compiler = new \Greg\View\BladeCompiler(__DIR__ . '/compiled');

Methods:

Includes Compiler Strategy methods.

compileFile

Compile a template file.

compileFile(string $file): string

compileString

Compile a template string.

compileString(string $string): string

addCompiler

Add a template compiler.

addCompiler(callable(string $content): string $compiler): $this

$compiler - A callable compiler.
    $content - Content to compile.

addDirective

Add a template directive.

addDirective(string $name, callable(string $args): string $compiler): $this

$name - Directive name;
$compiler - Directive callable.
    $args - Directive arguments.

addEmptyDirective

Add an empty template directive. See addDirective method.

addOptionalDirective

Add a template directive with optional parameters. See addDirective method.

Directives and template formats:

  • Display data
    • Secured - Display data throw htmlentities to prevent XSS attacks;
    • Raw - Display raw data;
    • Comments - Writing template comments.
  • Statements
  • Directives
    • Stop - Stop template execution.

Display secured data

Display data throw htmlentities to prevent XSS attacks.

Example:

Hello, {{ $name or 'guest' }}.

Display raw data

Display data as it is.

Example:

Hello, {!! $name or '<em>guest</em>' !!}.

Comments

Writing template comments.

Example:

{{-- This comment will not be present in the rendered HTML --}}

If Statement

Example:

@if (count($records) === 1)
    I have one record!
@elseif (count($records) > 1)
    I have multiple records!
@else
    I don't have any records!
@endif

Unless Statement

Example:

@unless (Auth::check())
    You are not signed in.
@elseunless (Auth::isVerified())
    You should verity your account.
@else
   Hello, {{ Auth::name() }}
@endunless

For Statement

Example:

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor

Foreach statement

Example:

@foreach ($users as $user)
    @continue($user->type == 1)

    <p>This is user {{ $user->id }}</p>

    @break($user->number == 5)
@empty
    <p>No users</p>
@endforeach

You 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>
@endforeach

The 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.

While statement

Example:

@while (true)
    <p>I'm looping forever.</p>
@endwhile

Switch statement

Example:

@switch ($color)
    @case('red')
        The color is red.
        
        @break
    @case('green')
        The color is green.

        @break
    @default
        The color is blue.
@endswitch

Verbatim statement

If you don't want to compile some content, you can use @verbatim statement.

Example:

@verbatim
    <div class="container">
        Hello, {{ name }}.
    </div>
@endverbatim

Stop

Stop executing the template.

Example:

I will be visible.

@stop

I will not be visible.