Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Preprocessor Loader that allows pre-filtering template code. #1508

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions lib/Twig/Loader/Preprocessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of Twig.
*
* (c) 2011 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Twig Preprocessor loader that allows adding custom text filters for template strings.
*
* For instance, you can make Twig produce more readable output by stripping leading
* spaces in lines with single control structure or comment:
*
* $loader = new Twig_Loader_Preprocessor($realLoader,
* function ($template) {
* return preg_replace('/^[ \t]*(\{([#%])[^}]*(?2)\})$/m', '$1', $template);
* }
* );
*
* See also twig issue #1005: https://github.com/fabpot/Twig/issues/1005
*
* @author Igor Tarasov <[email protected]>
*/
class Twig_Loader_Preprocessor implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
{
private $realLoader;
private $callback;

/**
* Constructor
*
* Callback should accept template string as the only argument and return the result
*
* @param Twig_LoaderInterface $loader A loader that does real loading of templates
* @param callable $callback The processing callback
*/
public function __construct(Twig_LoaderInterface $loader, $callback)
{
$this->realLoader = $loader;
$this->callback = $callback;
}

/**
* {@inheritdoc}
*/
public function getSource($name)
{
return call_user_func($this->callback, $this->realLoader->getSource($name));
}

/**
* {@inheritdoc}
*/
public function exists($name)
{
$name = (string) $name;

if ($this->realLoader instanceof Twig_ExistsLoaderInterface) {
return $this->realLoader->exists($name);
} else {
try {
$this->realLoader->getSource($name);

return true;
} catch (Twig_Error_Loader $e) {
}
}

return false;
}

/**
* {@inheritdoc}
*/
public function getCacheKey($name)
{
return $this->realLoader->getCacheKey($name);
}

/**
* {@inheritdoc}
*/
public function isFresh($name, $time)
{
return $this->realLoader->isFresh($name, $time);
}
}
36 changes: 36 additions & 0 deletions test/Twig/Tests/Loader/PreprocessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class Twig_Tests_Loader_PreprocessorTest extends PHPUnit_Framework_TestCase
{
public function testProcessing()
{
$realLoader = new Twig_Loader_String();
$loader = new Twig_Loader_Preprocessor($realLoader, 'strtoupper');
$this->assertEquals('TEST', $loader->getSource('test'));
}

public function testExists()
{
$realLoader = $this->getMock('Twig_Loader_Array', array('exists', 'getSource'), array(), '', false);
$realLoader->expects($this->once())->method('exists')->will($this->returnValue(false));
$realLoader->expects($this->never())->method('getSource');

$loader = new Twig_Loader_Preprocessor($realLoader, 'trim');
$this->assertFalse($loader->exists('foo'));

$realLoader = $this->getMock('Twig_LoaderInterface');
$realLoader->expects($this->once())->method('getSource')->will($this->returnValue('content'));

$loader = new Twig_Loader_Preprocessor($realLoader, 'trim');
$this->assertTrue($loader->exists('foo'));
}
}