addSourceAdapter() public method

If $pattern is a string, then must be a valid regex that matches the template filename. If $pattern is a callback, then must return true if the template is compilable, false otherwise.
public addSourceAdapter ( string | callback $pattern, Goetas\Twital\SourceAdapter $adapter ) : TwitalLoader
$pattern string | callback
$adapter Goetas\Twital\SourceAdapter
return TwitalLoader
 /**
  * Render a string
  *
  * A special option in the $locals array can be used to define the Twital adapter to use.
  * The array key is `__twital-adapter`, and the value is an instance of `\Goetas\Twital\SourceAdapter`
  *
  * @param string $template The template content to render
  * @param array $locals The variable to use in template
  * @return null|string
  *
  * @throws \Twig_Error_Loader
  * @throws \Twig_Error_Runtime
  * @throws \Twig_Error_Syntax
  */
 public function render($template, array $locals = array())
 {
     if (array_key_exists('__twital-adapter', $locals)) {
         $this->stringLoader->addSourceAdapter('/.*/', $locals['__twital-adapter']);
     }
     // Render the file using the straight string.
     $this->environment->setLoader($this->stringLoader);
     return $this->environment->render($template, $locals);
 }
 /**
  * Prepares the environment before running a test.
  */
 protected function setUp()
 {
     parent::setUp();
     $twitalLoader = new TwitalLoader(new StringLoader(), null, false);
     $twitalLoader->addSourceAdapter("/.*/", new XMLAdapter());
     $this->twig = new \Twig_Environment($twitalLoader);
 }
Beispiel #3
0
 public function testNonTwitalFile()
 {
     $twital = $this->getMock('Goetas\\Twital\\Twital');
     $twitalLoader = new TwitalLoader(new StringLoader(), $twital, false);
     $twitalLoader->addSourceAdapter('/.*\\.xml$/', new XMLAdapter());
     $twital->expects($this->never())->method('compile');
     $twitalLoader->getSource('aaa.txt');
 }
Beispiel #4
0
 protected function getTwital()
 {
     $loader = new \Twig_Loader_Array(array('index.html.twital' => '<div/>'));
     $twitalLoader = new TwitalLoader($loader);
     $twitalLoader->addSourceAdapter('/.*\\.twital/', new XMLAdapter());
     $twig = new \Twig_Environment($loader);
     $parser = new TemplateNameParser();
     $locator = $this->getMock('Symfony\\Component\\Config\\FileLocatorInterface');
     $twigEngine = new TwigEngine($twig, $parser, $locator);
     return new TwitalEngine($twigEngine, $parser);
 }