PHP code example of germania-kg / twigserviceprovider

1. Go to this page and download the library: Download germania-kg/twigserviceprovider 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/ */

    

germania-kg / twigserviceprovider example snippets



use Germania\TwigServiceProvider\TwigServiceProvider;
use Pimple\Container;

$pimple = new Container;
$pimple->register( new TwigServiceProvider );


$twig_environment = $pimple['Twig'];
echo $twig_environment->render('template', [ 'foo' => 'bar']);

$twig_environment = $pimple[ \Twig\Environment::class ];


$options = array(
	// For Twig's Filesystem Loader (string or array)
	'templates' => '/path/to/templates',

	// The most important Twig Environment options
	'debug' => false,
	'cache' => '/path/to/cache',
	'auto_reload' => true,
	'autoescape'  => false,
	'strict_variables' => false	
);


use Germania\TwigServiceProvider\TwigServiceProvider;

$custom_options = [
	'templates' => [ 
		'/another/path/to/templates',
		__DIR__ . '/vendor/foo/bar/templates'
	],
    'strict_variables' => true
];

$pimple->register( new TwigServiceProvider( $custom_options ));


$pimple->register( new TwigServiceProvider );

$pimple->extend('Twig.Config', function( $defaults, $pimple) {
	return array_merge( $defaults, [
		'templates'  => $pimple['custom.templates'],
		'strict_variables' => getenv('STRICT_ONLY')
	]);
});

// @return array
$pimple->extend('Twig.Options', function($options, $pimple) {
	return array_merge($options, [
		'charset' => 'iso-8859-1',
		'optimizations' => 0
	]);
});

// @return string
$pimple->extend('Twig.CachePath', function($old, $pimple) {
	return __DIR__ . '/var/cache';
});

// @return array
$pimple->extend('Twig.TemplatePaths', function($paths, $pimple) {
	return array_merge($paths, [
		'another/one',
		'vendor/name/package/templates'
	]);
});

// @return array
$pimple->extend('Twig.Loaders', function($loaders, $pimple) {
	return array_merge($loaders, [
			new Twig_Loader_Array( [ ... ] )
	]);
});

// @return array
$pimple->extend( 'Twig.Globals', ... );
$pimple->extend( 'Twig.Filters', ... );
$pimple->extend( 'Twig.Tests', ... );
$pimple->extend( 'Twig.Functions', ... );
$pimple->extend( 'Twig.Extensions', ... );