Skip to content

Latest commit

 

History

History
419 lines (280 loc) · 8.69 KB

Renderer.md

File metadata and controls

419 lines (280 loc) · 8.69 KB

Renderer Documentation

\Greg\View\Renderer is an instance of a template. Could be accessed via $this variable in the template.

Example:

<header>
    <?php echo $this->getSection("header")?>
</header>

<section class="content">
    <?php echo $this->content()?>
</section>

<footer>
    <?php echo $this->getSection("footer")?>
</footer>

Table of contents:

Magic methods:

__construct

Initialize the renderer.

__construct(Viewer $viewer, string $file, array $params = [])

$viewer - Viewer;
$file - Template file;
$params - Template parameters.

Example:

$viewer = new \Greg\View\Viewer(__DIR__ . '/views');

$renderer = new \Greg\View\ViewRenderer($viewer, __DIR__ . '/welcome.php', [
    'name' => 'Greg',
]);

Methods:

  • render - Render a template file with current parameters;
  • renderIfExists - Render a template file with current parameters if template exists;
  • renderString - Render a template string with current parameters;
  • renderStringIfExists - Render a template string with current parameters if template exists;
  • partial - Render a template file with new parameters;
  • partialIfExists - Render a template file with new parameters if template exists;
  • partialString - Render a template string with new parameters;
  • partialStringIfExists - Render a template string with new parameters if template exists;
  • each - Render a template file with current parameters for each value;
  • eachIfExists - Render a template file with current parameters for each value if template exists;
  • eachString - Render a template string with current parameters for each value;
  • eachStringIfExists - Render a template string with current parameters for each value if template exists;
  • extend - Extend template with another template file;
  • extendString - Extend template with another template string;
  • content - Get content;
  • section - Start or add a section;
  • parent - Get parent section;
  • endSection - End and register current section;
  • show - End and get current section;
  • getSection - Get a section;
  • push - Start a pusher or push contents in the stack;
  • endPush - End current pusher and add it to the stack;
  • stack - Get from the stack;
  • format - Execute a directive registered in the Viewer;
  • viewer - Get Viewer;
  • params - Get parameters;
  • file - Get file;
  • extended - Get extended template file;
  • setContent - Set content;
  • setSections - Set sections;
  • getSections - Get sections;
  • hasSection - Determine if a section exists;
  • setStacks - Set stacks;
  • getStacks - Get stacks;
  • hasStack - Determine if a stack exists;
  • __call - Execute a directive registered in the Viewer.

render

Render a template file with current parameters.

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

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

Example:

<?php echo $this->render('header', [
    'title' => 'I am a header!',
])?>

renderIfExists

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

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.

Example:

<?php echo $this->renderString('header.php', '<header><?php echo $title?></header>', [
    'title' => 'I am a header!',
])?>

renderStringIfExists

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

partial

Render a template file with new parameters.

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

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

partialIfExists

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

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.

partialStringIfExists

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

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

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 template exists. See eachString method.

extend

Extend template with another template file.

extend(string $name): $this

$name - Template file.

extendString

Extend template with another template string.

extend(string $id, string $string): $this

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

content

Get content.

content(): string

section

Start or add a section.

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

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

parent

Get parent section.

parent(): $this

endSection

End and register current section.

endSection(): $this

show

Finish and get current section content.

show(): string

getSection

Get a section.

getSection(string $name, string $else = null): string

$name - Section name;
$else - If the section does not exists, return this content.

push

Start a pusher or push contents in the stack.

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

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

endPush

End current pusher and add it to the stack.

endPush(): $this

stack

Get from the stack.

stack(string $name, string $else = null): string

$name - Stack name;
$else - If the stack doesn't exists, return this content.

format

Execute a directive registered in the Viewer.

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

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

viewer

Get Viewer.

viewer(): \Greg\View\Viewer

params

Get parameters.

params(): array

file

Get file.

file(): string

extended

Get extended template file.

extended(): string

setContent

Set content.

setContent(string $content): $this

$content - Registered content.

setSections

Set sections.

setSections(array $sections): $this

$sections - Sections.

getSections

Get sections.

getSections(): array

hasSection

Determine if a section exists.

hasSection(string $name): boolean

$name - Section name.

setStacks

Set stacks.

setStacks(array $stacks): $this

$stacks - Stacks.

getStacks

Get stacks.

getStacks(): array

hasStack

Determine if a stack exists.

hasStack(string $name): boolean

$name - Stack name.