Adding Twig: functions


#1

Hi Guys,

Last hour I tried to add some (basic) functions from Twig (like the Dump and Template from String). Somewhere i am missing some info how to add this…

I understand: ‘The template_from_string function is not available by default. You must add the Twig_Extension_StringLoader extension explicitly when creating your Twig environment:’. – But not sure how to implement this.

My Twig/Views setup:

$container['view'] = function($container) {
	$view = new \Slim\Views\Twig(__DIR__ . '/../resources/views', [
		'cache' => false,
	]);

	$view->addExtension(new \Slim\Views\TwigExtension(
		$container->router,
		$container->request->getUri()
	));

	$view->getEnvironment()->addGlobal('flash', $container->flash);


	// Not sure how (or if) to implement this here?
        $twig = new Twig_Environment($loader, array(
              'debug' => true,
        ));
        $twig->addExtension(new Twig_Extension_Debug());
        $twig->addExtension(new Twig_Extension_StringLoader());


	return $view;
};


#2

Stupid. Solved 1/2 of it (by mistake…)

$view->addExtension(new Twig_Extension_StringLoader());

#3

I use the following:

    $view->addExtension(new SpecialExtensions($container));

And my special stuff:

<?php

namespace SlimDemo\Common\Helpers;

class SpecialExtensions extends \Twig_Extension
{
	protected $container;

	public function __construct($container)
	{
		$this->container = $container;
	}

	public function getFunctions()
	{
		return array(
			new \Twig_SimpleFunction('showError', array($this, 'showError'), array('is_safe' => array('html'))),
			new \Twig_SimpleFunction('test', array($this, 'test')),
		);
	}

	/**
	 * Show the errors of a specific form field
	 *
	 * Example:
	 * {{ showError('name') }}
	 *
	 * @param $field
	 *
	 * @return string
	 */
	public function showError($field)
	{
		$errorText = '';

		if ($this->app->offsetExists('errors'))
		{
			if ($field !== '*')
			{
				if (array_key_exists($field, $this->app['errors']))
				{
					$appErrors = $this->app['errors'];
					$aErrors = $appErrors[$field];
					unset($appErrors[$field]);
					$this->app['errors'] = $appErrors;

					foreach ($aErrors as $errorline)
					{
						$errorText .= $errorline[1] . '<BR>';
					}
				}
			}
			else
			{
				// if there are any errors left, that where not asked for in the template, spit them out now
				foreach ($this->app['errors'] as $key => $error)
				{
					// @codeCoverageIgnoreStart
					$errorText .= 'Because of a wrong validationCondition in this Entity, you see this error:<BR>';
					foreach ($error as $errorline)
					{
						$errorText .= $key . ' => ' . $errorline[1] . '<BR>';
					} // @codeCoverageIgnoreEnd
				}
			}

			if (substr($errorText, -4) == '<BR>')
			{
				$errorText = "<div class='error'>" . substr($errorText, 0, - 4) . '</div>';
			}
		}

		return $errorText;
	}

	/**
	 * Does a reverse strin echo
	 *
	 * @param $value - the id to search
	 *
	 * Example:
	 * {{ test('hello world') }}
	 */
	public function test($text)
	{
		return strrev($text);
	}

	public function getName()
	{
		return 'myFunctions';
	}

}

#4

I USE LIKE THiS:

use Slim\Views\Twig;
use Slim\Views\TwigExtension;


$container['view'] = function ($container){
        
    $twig = new Twig(__DIR__.'/../resources/views',[
        
        'cache'=>false,
        
    ]);
    
    $twig->addExtension(new TwigExtension(
        
        $container->get('router'),
        $container->get('request')->getUri()
        
    ));
    
    $twig->getEnvironment()->addGlobal('currentUrl',$container->get('request')->getUri());
    $twig->getEnvironment()->addGlobal('currentUri',$container->get('request')->getUri()->getPath());
    
    return $twig;
};