本文整理汇总了PHP中Twig_Environment::addExtension方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::addExtension方法的具体用法?PHP Twig_Environment::addExtension怎么用?PHP Twig_Environment::addExtension使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::addExtension方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: register
public function register(Application $app)
{
$app['twig.options'] = array();
$app['twig.form.templates'] = array('form_div_layout.html.twig');
$app['twig.path'] = array();
$app['twig.templates'] = array();
$app['twig'] = $app->share(function ($app) {
$app['twig.options'] = array_replace(array('charset' => "UTF-8", 'debug' => isset($app['debug']) ? $app['debug'] : false, 'strict_variables' => isset($app['debug']) ? $app['debug'] : false), $app['twig.options']);
$twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']);
$twig->addGlobal('app', $app);
$twig->addExtension(new TwigCoreExtension());
if (isset($app['debug']) && $app['debug']) {
$twig->addExtension(new \Twig_Extension_Debug());
}
return $twig;
});
$app['twig.loader.filesystem'] = $app->share(function ($app) {
return new \Twig_Loader_Filesystem($app['twig.path']);
});
$app['twig.loader.array'] = $app->share(function ($app) {
return new \Twig_Loader_Array($app['twig.templates']);
});
$app['twig.loader.string'] = $app->share(function ($app) {
return new \Twig_Loader_String();
});
$app['twig.loader'] = $app->share(function ($app) {
return new \Twig_Loader_Chain(array($app['twig.loader.filesystem'], $app['twig.loader.array'], $app['twig.loader.string']));
});
}
示例2: __construct
/**
* @param Configuration $configuration
* @param ServiceManager $serviceManager
*/
public function __construct(Configuration $configuration, ServiceManager $serviceManager)
{
if (!$configuration->get('twig', null)) {
return;
}
$cachePath = $configuration->getApplicationPath() . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
$options = [];
if ($configuration->get('cache.config.enabled', false)) {
$options['cache'] = $cachePath;
}
$paths = $configuration->get('twig.paths', []);
foreach ($paths as $offset => $path) {
$paths[$offset] = dirname($configuration->getApplicationPath()) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $path;
}
$loader = new \Twig_Loader_Filesystem($paths);
$this->twig = new \Twig_Environment($loader, $options + $configuration->get('twig.options', []));
if (isset($configuration->get('twig.options', [])['debug']) && $configuration->get('twig.options', [])['debug']) {
$this->twig->addExtension(new \Twig_Extension_Debug());
}
foreach ($configuration->get('twig.helpers', []) as $functionName => $helperClass) {
$func = new \Twig_SimpleFunction($functionName, function () use($helperClass, $serviceManager) {
$instance = $serviceManager->load($helperClass);
return call_user_func_array($instance, func_get_args());
});
$this->twig->addFunction($func);
}
foreach ($configuration->get('twig.filters', []) as $functionName => $helperClass) {
$func = new \Twig_SimpleFilter($functionName, function () use($helperClass, $serviceManager) {
$instance = $serviceManager->load($helperClass);
return call_user_func_array($instance, func_get_args());
});
$this->twig->addFilter($func);
}
}
示例3: testIntegration
/**
* @dataProvider getTests
*/
public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
{
if ($condition) {
eval('$ret = ' . $condition . ';');
if (!$ret) {
$this->markTestSkipped($condition);
}
}
$loader = new Twig_Loader_Array($templates);
foreach ($outputs as $match) {
$config = array_merge(array('cache' => false, 'strict_variables' => true), $match[2] ? eval($match[2] . ';') : array());
$twig = new Twig_Environment($loader, $config);
$twig->addExtension(new TestExtension());
$twig->addExtension(new Twig_Extension_Debug());
$policy = new Twig_Sandbox_SecurityPolicy(array(), array(), array(), array(), array());
$twig->addExtension(new Twig_Extension_Sandbox($policy, false));
$twig->addGlobal('global', 'global');
try {
$template = $twig->loadTemplate('index.twig');
} catch (Exception $e) {
if (false !== $exception) {
$this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
return;
}
if ($e instanceof Twig_Error_Syntax) {
$e->setTemplateFile($file);
throw $e;
}
throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
}
try {
$output = trim($template->render(eval($match[1] . ';')), "\n ");
} catch (Exception $e) {
if (false !== $exception) {
$this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
return;
}
if ($e instanceof Twig_Error_Syntax) {
$e->setTemplateFile($file);
} else {
$e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
}
$output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
}
if (false !== $exception) {
list($class, ) = explode(':', $exception);
$this->assertThat(NULL, new PHPUnit_Framework_Constraint_Exception($class));
}
$expected = trim($match[3], "\n ");
if ($expected != $output) {
echo 'Compiled template that failed:';
foreach (array_keys($templates) as $name) {
echo "Template: {$name}\n";
$source = $loader->getSource($name);
echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
}
}
$this->assertEquals($expected, $output, $message . ' (in ' . $file . ')');
}
}
示例4: init
public function init()
{
$loader = $this->getTwigFilesystemLoader();
$this->environment = new Twig_Environment($loader, ['debug' => $this->config->get('twig.debug'), 'cache' => $this->config->get('twig.cache')]);
if (!$this->config->isEmpty('twig.debug')) {
$this->environment->addExtension(new Twig_Extension_Debug());
}
$this->environment->addExtension(new HerbieExtension());
$this->addTwigPlugins();
foreach (Hook::trigger(Hook::CONFIG, 'addTwigFunction') as $function) {
try {
list($name, $callable, $options) = $function;
$this->environment->addFunction(new \Twig_SimpleFunction($name, $callable, (array) $options));
} catch (\Exception $e) {
/*do nothing else yet*/
}
}
foreach (Hook::trigger(Hook::CONFIG, 'addTwigFilter') as $filter) {
try {
list($name, $callable, $options) = $filter;
$this->environment->addFilter(new \Twig_SimpleFilter($name, $callable, (array) $options));
} catch (\Exception $e) {
/*do nothing else yet*/
}
}
foreach (Hook::trigger(Hook::CONFIG, 'addTwigTest') as $test) {
try {
list($name, $callable, $options) = $test;
$this->environment->addTest(new \Twig_SimpleTest($name, $callable, (array) $options));
} catch (\Exception $e) {
/*do nothing else yet*/
}
}
$this->initialized = true;
}
示例5: configureLayout
/**
* @param string $path Relative path to layout file
*/
protected function configureLayout($path)
{
$loader = new Loader([str_replace('tests', 'src', __DIR__)]);
$this->environment = new Environment($loader, ['debug' => true, 'strict_variables' => true]);
$this->renderer = new FormRenderer(new FormTheme($this->environment, $path));
$this->environment->addExtension(new FormExtension($this->renderer));
}
示例6: setUpTwig
/**
* Create twig environment and add the extension.
*/
protected function setUpTwig()
{
$this->twig = new Twig_Environment(new Twig_Loader_Filesystem(array(self::getTemplatesPath())));
$this->componentExtension = new ComponentExtension();
$this->componentExtension->addComponent(new NamedComponent('test'));
$this->twig->addExtension($this->componentExtension);
}
示例7: setUp
protected function setUp()
{
$this->env = new \Twig_Environment(
new \Twig_Loader_Array(
array(
'state' => '{{ finite_state(object) }}',
'transitions' => '{% for transition in finite_transitions(object) %}{{ transition }}{% endfor %}',
'properties' => '{% for property, val in finite_properties(object) %}{{ property }}{% endfor %}',
'has' => '{{ finite_has(object, property) ? "yes" : "no" }}',
'can' => '{{ finite_can(object, transition) ? "yes" : "no" }}'
)
)
);
$container = new \Pimple(array(
'state_machine' => function() {
$sm = new StateMachine;
$sm->addState(new State('s1', State::TYPE_INITIAL, array(), array('foo' => true, 'bar' => false)));
$sm->addTransition('t12', 's1', 's2');
$sm->addTransition('t23', 's2', 's3');
return $sm;
}
));
$this->context = new Context(new PimpleFactory($container, 'state_machine'));;
$this->env->addExtension(new FiniteExtension($this->context));
}
示例8: setUp
public function setUp()
{
$this->loader = new \Twig_Loader_Filesystem(array(realpath(__DIR__ . '/../Resources/twig')));
$this->environment = new \Twig_Environment($this->loader, array());
$this->environment->addExtension(new Extension(array('nodes' => array('RunOpenCode\\Twig\\BufferizedTemplate\\Tests\\Mockup\\DummyTwigNode' => 20))));
$this->environment->addExtension(new DummyTwigExtension());
}
示例9: setUp
protected function setUp()
{
$loader = new \Twig_Loader_Array(array());
$this->extension = new TwigExtension();
$this->twig = new \Twig_Environment($loader);
$this->twig->addExtension($this->extension);
}
示例10: setUp
/**
* {@inheritdoc}
*/
public function setUp()
{
parent::setUp();
if (!in_array($this->type, array('form', 'filter'))) {
throw new \Exception('Please override $this->type in your test class specifying template to use (either form or filter)');
}
$rendererEngine = new TwigRendererEngine(array($this->type . '_admin_fields.html.twig'));
$csrfManagerClass = interface_exists('Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface') ? 'Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface' : 'Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface';
$renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfManagerClass));
$this->extension = new FormExtension($renderer);
$twigPaths = array(__DIR__ . '/../../../Resources/views/Form');
//this is ugly workaround for different build strategies and, possibly,
//different TwigBridge installation directories
if (is_dir(__DIR__ . '/../../../vendor/symfony/twig-bridge/Resources/views/Form')) {
$twigPaths[] = __DIR__ . '/../../../vendor/symfony/twig-bridge/Resources/views/Form';
} elseif (is_dir(__DIR__ . '/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form')) {
$twigPaths[] = __DIR__ . '/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
} else {
$twigPaths[] = __DIR__ . '/../../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
}
$loader = new StubFilesystemLoader($twigPaths);
$this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
$this->environment->addGlobal('sonata_admin', $this->getSonataAdmin());
$this->environment->addExtension(new TranslationExtension(new StubTranslator()));
$this->environment->addExtension($this->extension);
$this->extension->initRuntime($this->environment);
}
示例11: __invoke
/**
* @param ContainerInterface $container
* @return TwigRenderer
*/
public function __invoke(ContainerInterface $container)
{
$config = $container->has('config') ? $container->get('config') : [];
$debug = array_key_exists('debug', $config) ? (bool) $config['debug'] : false;
$config = isset($config['templates']) ? $config['templates'] : [];
$cacheDir = isset($config['cache_dir']) ? $config['cache_dir'] : false;
// Create the engine instance
$loader = new TwigLoader();
$environment = new TwigEnvironment($loader, ['cache' => $debug ? false : $cacheDir, 'debug' => $debug, 'strict_variables' => $debug, 'auto_reload' => $debug]);
// Add extensions
if ($container->has(RouterInterface::class)) {
$environment->addExtension(new TwigExtension($container->get(RouterInterface::class), isset($config['assets_url']) ? $config['assets_url'] : '', isset($config['assets_version']) ? $config['assets_version'] : ''));
}
if ($debug) {
$environment->addExtension(new TwigExtensionDebug());
}
// Inject environment
$twig = new TwigRenderer($environment, isset($config['extension']) ? $config['extension'] : 'html.twig');
// Add template paths
$allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : [];
foreach ($allPaths as $namespace => $paths) {
$namespace = is_numeric($namespace) ? null : $namespace;
foreach ((array) $paths as $path) {
$twig->addPath($path, $namespace);
}
}
return $twig;
}
示例12: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array(__DIR__ . '/../../Resources/views/Form')));
$this->twig->addExtension(new CKEditorExtension($this->helper));
$this->template = $this->twig->loadTemplate('ckeditor_widget.html.twig');
}
示例13: init
public function init()
{
$view_dir = [__DIR__ . '/../Views', __DIR__ . '/../Templates'];
$twigConfig = [];
if ($this->config['twig']['cache']) {
$twigConfig["cache"] = $this->app['cache']['twig'];
}
$twigConfig["debug"] = $this->config['twig']['debug'];
$loader = new \Twig_Loader_Filesystem($view_dir);
foreach ($view_dir as $d) {
$loader->addPath($d, 'Meister');
}
foreach ($this->config['modules'] as $app) {
if (file_exists($this->app['Modules'] . $app . '/Views')) {
$loader->addPath($this->app['Modules'] . $app . '/Views', $app);
}
if (file_exists($this->app['Modules'] . $app . '/Templates')) {
$loader->addPath($this->app['Modules'] . $app . '/Templates', $app);
}
}
$this->twig = new \Twig_Environment($loader, $twigConfig);
$this->twig->addExtension(new \Twig_Extensions_Extension_I18n());
/**
* Verifica permissões para exibir determinada coisa
*/
$function = new \Twig_SimpleFunction('permission', function ($rule) {
return $this->app['auth']->checkRules($rule);
});
$this->twig->addFunction($function);
}
示例14: array
function __construct()
{
// This is how to call the template engine:
// do_action('wunderground_render_template', $file_name, $data_array );
add_action('wunderground_render_template', array(&$this, 'render'), 10, 2);
// Set up Twig
Twig_Autoloader::register();
// This path should always be the last
$base_path = trailingslashit(plugin_dir_path(Wunderground_Plugin::$file)) . 'templates';
$this->loader = new Twig_Loader_Filesystem($base_path);
// Tap in here to add additional template paths
$additional_paths = apply_filters('wunderground_template_paths', array(trailingslashit(get_stylesheet_directory()) . 'wunderground'));
foreach ($additional_paths as $path) {
// If the directory exists
if (is_dir($path)) {
// Tell Twig to use it first
$this->loader->prependPath($path);
}
}
// You can force debug mode by adding `add_filter( 'wunderground_twig_debug' '__return_true' );`
$debug = apply_filters('wunderground_twig_debug', current_user_can('manage_options') && isset($_GET['debug']));
$this->twig = new Twig_Environment($this->loader, array('debug' => !empty($debug), 'auto_reload' => true));
if (!empty($debug)) {
$this->twig->addExtension(new Twig_Extension_Debug());
}
}
示例15: setUp
public function setUp()
{
$this->cmfHelper = $this->getMockBuilder('Symfony\\Cmf\\Bundle\\CoreBundle\\Templating\\Helper\\CmfHelper')->disableOriginalConstructor()->getMock();
$this->cmfExtension = new CmfExtension($this->cmfHelper);
$this->env = new \Twig_Environment(new \Twig_Loader_Array(array()));
$this->env->addExtension($this->cmfExtension);
}