Skip to content

Latest commit

 

History

History
425 lines (283 loc) · 7.79 KB

Viewer.md

File metadata and controls

425 lines (283 loc) · 7.79 KB

Viewer Documentation

\Greg\View\Viewer is the view manager.

Extends: \ArrayAccess.

Example:

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

echo $viewer->render('welcome', [
    'name' => 'Greg',
]);

Table of contents:

Magic methods:

__construct

Initialize the viewer.

__construct(string|array $path, array $params = [])

$path - Templates directory;
$params - This parameters will be assigned in all templates.

Example:

$viewer = new \Greg\View\Viewer(__DIR__ . '/views', [
    'repository' => 'greg-md/php-view',
]);

Methods:

getCompiledFile

Get compiled file by a template file.

getCompiledFile(string $name): string

$name - Template name.

getCompiledFileFromString

Get compiled file by a template string.

getCompiledFile(string $id, string $string): string

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

render

Render a template file.

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

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

Example:

echo $viewer->render('welcome', [
    'name' => 'Greg',
]);

renderIfExists

Render a template file if exists. See render method.

renderString

Render a template string.

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

$id - Template unique id. It should has the compiler extension;
$string - Template string;
$params - Template parameters. Will be available only in current template.

Example:

echo $viewer->renderString('welcome.blade.php', "Hello {{ $name }}!", [
    'name' => 'Greg',
]);

renderStringIfExists

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

assign

Assign a parameter to all templates.

assign(string $key, string $value): $this

$key - Parameter key;
$value - Parameter value.

Example:

$viewer->assign('author', 'Greg');

assignMultiple

Assign multiple parameters to all templates.

assignMultiple(array $params): $this

$params - An array of parameters;

Example:

$viewer->assignMultiple([
    'position' => 'Web Developer',
    'website' => 'http://greg.md/',
]);

assigned

Get assigned parameters.

assigned(string|array $key = null): any

$key - Parameter key or an array of keys;

Example:

$all = $viewer->assigned();

$foo = $viewer->assigned('foo');

hasAssigned

Determine if assigned parameters exists.

hasAssigned(string|array $key = null): boolean

$key - Parameter key or an array of keys;

Example:

if ($viewer->hasAssigned()) {
    // Has assigned parameters.
}

if ($viewer->hasAssigned('foo')) {
    // Has assigned parameter 'foo'.
}

removeAssigned

Remove assigned parameters.

removeAssigned(string|array $key = null): $this

$key - Parameter key or an array of keys;

Example:

// Delete 'foo' parameter.
$viewer->removeAssigned('foo');

// Delete 'foo' and 'baz' parameters.
$viewer->removeAssigned(['bar', 'baz']);

// Delete all parameters.
$viewer->removeAssigned();

setPaths

Replace templates directories.

setPaths(array $paths): $this

$paths - Templates directories.

Example:

$viewer->setPaths([
    __DIR__ . '/views',
]);

addPaths

Add templates directories. See setPaths method.

addPaths(array $paths): $this

addPath

Add a template directory.

addPath(string $path): $this

$path - Template directory.

Example:

$viewer->addPath(__DIR__ . '/views');

getPaths

Get templates directories.

getPaths(): array

addExtension

Add an extension, optionally with a compiler.

addExtension(string $extension, \Greg\View\CompilerInterface|callable(): \Greg\View\CompilerInterface $compiler = null): $this

$extension - Template extension;
$compiler - Template compiler.

Example:

$viewer->addExtension('.template');

$viewer->addExtension('.blade.php', function (\Greg\View\Viewer $viewer) {
    return new \Greg\View\ViewBladeCompiler($viewer, __DIR__ . '/compiled');
});

getExtensions

Get all known extensions.

getExtensions(): string[]

getSortedExtensions

Get all known extensions in good a sorted way.

getExtensions(): string[]

hasCompiler

Determine if a compiler exists by extension.

hasCompiler(string $extension): boolean

getCompiler

Get compiler by extension.

getCompiler(string $extension): \Greg\View\CompilerInterface

$extension - Template extension.

Returns an interface of \Greg\View\CompilerInterface.

Example:

$compiler = $viewer->getCompiler('.blade.php');

$file = $compiler->getCompiledFile();

getCompilers

Get all registered compilers.

getCompilers(): \Greg\View\CompilerInterface[]

Returns an array of \Greg\View\CompilerInterface interfaces.

getCompilersExtensions

Get all extensions which has compilers.

getCompilersExtensions(): string[]

removeCompiledFiles

Remove all compiled files from compilers compilation path.

removeCompiledFiles(): $this

directive

Register a directive.

directive(string $name, callable(mixed ...$args): string $callable): $this

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

Example:

$viewer->directive('alert', function($message) {
    echo '<script>alert("' . $message . '");</script>';
});

hasDirective

Determine if a directive exists.

hasDirective(string $name): boolean

format

Execute a directive.

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

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

Example:

$viewer->format('alert', 'I am an alert message!');