Skip to content

Latest commit

 

History

History
389 lines (249 loc) · 6.81 KB

ViewBladeCompiler.md

File metadata and controls

389 lines (249 loc) · 6.81 KB

View Blade Compiler Documentation

\Greg\View\ViewBladeCompiler is an extended Blade Compiler, specially for the Viewer Contract.

Extends: \Greg\View\BladeCompiler.

Implements: \Greg\View\ViewCompilerStrategy.

Table of contents:

Methods:

Includes Blade Compiler methods.

addViewDirective

Add a directive that was already registered in the Viewer, but not in the compiler.

addViewDirective(string $name): $this

$name - Directive name;

Example:

$compiler->addViewDirective('alert');

Directives and template formats:

Includes Blade Compiler directives and template formats.

  • Statements
  • Directives
    • Extends - Extend template;
    • Render - Render another template with existing parameters.
    • Partial - Render another template with new parameters;
    • Each - Render template for each element in array;
    • Content - Get registered content;
    • Format - Execute a directive registered in the Viewer Contract.

Section Statement

section

Start or add a section;

section(string $name, string $content = null)

$name - Section name;
$content - Section content.

endsection

End and register current section.

endsection()

yield

Display a section.

yield(string $name): string

$name - Section name.

parent

Display parent section.

parent(): string

show

End and display current section.

show(): string

Example:

Example 1:

@section("hello-world", "Hello")

@section("hello-world")
    @parent World!
@endsection

@yield("hello-world")

Output:

Hello World!

Example 2:

@section("hello-world")
    Hello
@endsection

@section("hello-world")
    @parent World!
@show

Output:

Hello World!

Push Statement

push

Start a pusher or push contents in the stack.

push(string $name, string $content = null)

$name - Stack name;
$content - Stack content.

endpush

End current pusher and add it to the stack.

endpush()

stack

Display contents from the stack.

stack(string $name): string

$name - Stack name.

Example:

@push("js", "<script>alert('Foo')</script>")

@push("js")
    <script>alert('Bar')</script>
@endpush

@stack("js")

Output:

<script>alert('Foo')</script>
<script>alert('Bar')</script>

Extends

extends

Extend template with another template file.

extends(string $name)

$name - Template file.

extendsString

Extend template with another template string.

extendsString(string $id, string $string)

$id - Template unique id. It should has the compiler extension;
$string - Template string.

content

Display parent content.

content(): string

Example:

Create a template layout.blade.php:

<section class="content">
    @content
</section>

Extend layout template:

@extends("layout")

Hello World!

Output:

<section class="content">
    Hello World!
</section>

Render

render

Render a template file with current parameters.

render(string $name, array $params = []): string

$name - Template file;
$params - Template custom parameters.

renderIfExists

Render a template file with current parameters if template exists. See render directive.

renderString

Render a template string with current parameters.

renderString(string $id, string $string, array $params = []): string

$id - Template unique id. It should has the compiler extension;
$string - Template string;
$params - Template custom parameters.

renderStringIfExists

Render a template string with current parameters if its compiler exists. See renderString directive.

Example:

@render("foo")

@renderIfExists("bar")

Partial

partial

Render a template file with new parameters.

partial(string $name, array $params = []): string

$name - Template file;
$params - Template custom parameters.

partialIfExists

Render a template file with new parameters if template exists. See partial directive.

partialString

Render a template string with new parameters.

partialString(string $id, string $string, array $params = []): string

$id - Template unique id. It should has the compiler extension;
$string - Template string;
$params - Template custom parameters.

partialIfExists

Render a template string with new parameters if its compiler exists. See partialString directive.

Example:

@partial("foo")

@partialIfExists("bar")

Each

each

Render a template file with current parameters for each value.

each(string $name, array $values, array $params = [], string $valueKeyName = null, string $emptyName = null): string

$name - Template file;
$values - Values;
$params - Template custom parameters;
$valueKeyName - The key name of the current value;
$emptyName - If no values, will render this template file.

eachIfExists

Render a template file with current parameters for each value if template exists. See each directive.

eachString

Render a template string with current parameters for each value.

eachString(string $id, string $string, array $values, array $params = [], string $valueKeyName = null, string $emptyId = null, string $emptyString = null): string

$id - Template unique id. It should has the compiler extension;
$string - Template string;
$values - Values;
$params - Template custom parameters;
$valueKeyName - The key name of the current value;
$emptyId - Template unique id. Will use it if no values found; $emptyString - Template string. Will use it if no values found.

eachStringIfExists

Render a template string with current parameters for each value if its compiler exists. See eachString directive.

Example:

@each("foo", [1, 2])

@renderIfExists("bar", [1, 2])

Format

Execute a directive registered in the Viewer Contract.

format(string $name, mixed ...$args)

Example:

@format("alert", "I am a javascript alert!")

<!-- or -->

@alert("I am a javascript alert!")