Skip to content

Latest commit

 

History

History
802 lines (486 loc) · 15.7 KB

Twig.md

File metadata and controls

802 lines (486 loc) · 15.7 KB

b2r\Component\Twig\Twig

Flags: class

Index

Uses | Methods

Twig composition

Features

  • Fluent interface
  • Smart loader
  • Context container

Usage

use b2r\Component\Twig\Twig;

$twig = new Twig();

$twig->template('hello', 'Hello, {{ name }}');
echo $twig->name('world'); #=>'Hello, world'
$twig = new Twig(__DIR__ . '/templates');

$twig->addSuffix('.twig');

$twig->addFilters([
    'l' => function ($value) {
        return strtolower($value);
    },
    'u' => function ($value) {
        return strtoupper($value);
    },
]);

$twig->addFunctions([
    'x2' => function ($value) {
        return $value * 2;
    },
]);

$twig->addGlobals([
    'foo' => 'FOO',
    'bar' => 'BAR',
    'baz' => 'BAZ',
]);

echo $twig->template('hello')->name('world');

hello.twig

Hello, {{ name }}
Hello, {{ name|u }}
Hello, {{ name|l }}
Hello, {{ foo }}
Hello, {{ bar }}
Hello, {{ baz }}
Score: {{ x2(100) }}

output

Hello, world
Hello, WORLD
Hello, world
Hello, FOO
Hello, BAR
Hello, BAZ
Score: 200

Uses

  • b2r\Component\SimpleAccessor\Getter
  • b2r\Component\Twig\Traits\EnvironmentComposition
  • b2r\Component\Twig\Traits\LoaderComposition

Methods


__call

public function __call($name, $arguments): mixed

Flags: public

Parameters

name type default description
$name string
$arguments array

Magic method

  • @throws b2r\Component\Exception\InvalidMethodException
  • @invoke bindValue

(Back to index)


__construct

public function __construct($paths, $options)

Flags: public constructor

Parameters

name type default description
$paths []
$options array []

Constructor

(Back to index)


__get

public function __get($name)

Flags: public

Parameters

name type default description
$name

(Back to index)


__set

public function __set($name, $value): self

Flags: public

Parameters

name type default description
$name string Context name
$value mixed Context value

Magic setter

  • Assume context value setter

  • @alias bindValue

(Back to index)


__toString

public function __toString()

Flags: public

(Back to index)


addContext

public function addContext()

Flags: public

(Back to index)


addExtension

public function addExtension($extension): self

Flags: public

Parameters

name type default description
$extension Twig_Extension, string Extension instance or class name

(Back to index)


addExtensions

public function addExtensions($extensions): self

Flags: public

Parameters

name type default description
$extensions iterable [Twig_Extension $instance|string $className]

(Back to index)


addFilter

public function addFilter($name, $filter): self

Flags: public

Parameters

name type default description
$name string, Twig_Filter
$filter Closure, Twig_Filter null

(Back to index)


addFilters

public function addFilters($filters): self

Flags: public

Parameters

name type default description
$filters iterable [string $name => Twig_SimpleFilter|Closure $value]

(Back to index)


addFunction

public function addFunction($name, $function): self

Flags: public

Parameters

name type default description
$name string, Twig_Function
$function Closure, Twig_Function null

(Back to index)


addFunctions

public function addFunctions($functions): self

Flags: public

Parameters

name type default description
$functions iterable [string $name => Twig_SimpleFunction|Closure $function]

(Back to index)


addGlobal

public function addGlobal($name, $value): self

Flags: public

Parameters

name type default description
$name
$value

(Back to index)


addGlobals

public function addGlobals($globals): self

Flags: public

Parameters

name type default description
$globals iterable [string $name => mixed $value]

(Back to index)


addSuffix

public function addSuffix($suffix)

Flags: public

Parameters

name type default description
$suffix string

(Back to index)


addSuffixes

public function addSuffixes($suffixes)

Flags: public

Parameters

name type default description
$suffixes array

(Back to index)


addTemplate

public function addTemplate($name, $template)

Flags: public

Parameters

name type default description
$name string
$template string

(Back to index)


addTemplates

public function addTemplates($templates)

Flags: public

Parameters

name type default description
$templates array

(Back to index)


bind

public function bind(): self

Flags: public

Bind context value

Parameters

  • (string $name, mixed $value)

  • (array $values)

  • @throws b2r\Component\Exception\InvalidArgumentException

  • @invoke bindValue

  • @invoke bindArray

(Back to index)


bindArray

public function bindArray($values): self

Flags: public

Parameters

name type default description
$values array

(Back to index)


bindValue

public function bindValue($name, $value): self

Flags: public

Parameters

name type default description
$name string
$value mixed

(Back to index)


clearContext

public function clearContext(): self

Flags: public

(Back to index)


context

public function context()

Flags: public

(Back to index)


display

public function display($name, $context)

Flags: public

Parameters

name type default description
$name string null Template name
$context array []
  • @invoke Twig_Environment::display

(Back to index)


getArrayLoader

public function getArrayLoader(): Twig_Loader_Array

Flags: public

(Back to index)


getContext

public function getContext(): array

Flags: public

(Back to index)


getContextValue

public function getContextValue($name, $default): mixed

Flags: public

Parameters

name type default description
$name string
$default null

(Back to index)


getEngine

public function getEngine(): Twig_Environment

Flags: public

(Back to index)


getEnv

public function getEnv(): Twig_Environment

Flags: public

(Back to index)


getEnvironment

public function getEnvironment(): Twig_Environment

Flags: public

(Back to index)


getFileLoader

public function getFileLoader(): b2r\Component\Twig\Loader\FilesystemLoader

Flags: public

(Back to index)


getFilesystemLoader

public function getFilesystemLoader(): b2r\Component\Twig\Loader\FilesystemLoader

Flags: public

(Back to index)


getLoader

public function getLoader(): Twig_Loader_Chain

Flags: public

(Back to index)


getTemplateName

public function getTemplateName(): string

Flags: public

(Back to index)


params

public function params()

Flags: public

(Back to index)


render

public function render($name, $context): string

Flags: public

Parameters

name type default description
$name string null Template name
$context array []

Render

  • @invoke Twig_Environment::render

(Back to index)


save

public function save($filename, $name, $context): self

Flags: public

Parameters

name type default description
$filename string
$name string null
$context array []

Save renderd string to file

(Back to index)


set

public function set()

Flags: public

(Back to index)


setContextValue

public function setContextValue($name, $value)

Flags: public

Parameters

name type default description
$name string
$value

(Back to index)


setTemplate

public function setTemplate($name, $source): self

Flags: public

Parameters

name type default description
$name string Template name
$source string null Template source

Set template

(Back to index)


setTemplates

public function setTemplates($templates): self

Flags: public

Parameters

name type default description
$templates array [string $name => string $template]

Set templates

(Back to index)


template

public function template($name, $source)

Flags: public

Parameters

name type default description
$name string
$source string null

(Back to index)


toString

public function toString(): string

Flags: public

(Back to index)


unsetContextValue

public function unsetContextValue($name): self

Flags: public

Parameters

name type default description
$name string

(Back to index)