PHP code example of b2r / twig

1. Go to this page and download the library: Download b2r/twig library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

b2r / twig example snippets


use b2r\Component\Twig\Twig;

$twig = new Twig();

echo $twig
    ->template('hello', 'Hello, {{ name }}') // Define 'hello' template and 'hello' as current template
    ->name('world') // Set name context
    ; #=>'Hello, world' invoke __toString()


use b2r\Component\Twig\Twig;

$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');