Compiles a Twital template into a Twig template.
/** * 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); }
function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue) { if ($file->getExtension() == 'twital' && ($adapter = $this->twitalLoader->getSourceAdapter((string) $file))) { $source = $this->twitalLoader->getTwital()->compile($adapter, file_get_contents((string) $file)); $ast = $this->twig->parse($this->twig->tokenize($source, (string) $file)); $this->visitTwigFile($file, $catalogue, $ast); } }
/** * 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); }
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); }
public function testNonExistsWithBaseLoader() { $mockLoader = $this->getMock('Twig_LoaderInterface'); $mockLoader->expects($this->once())->method('getSource')->with($this->equalTo('foo'))->will($this->throwException(new \Twig_Error_Loader("File not found"))); $mockLoader->expects($this->never())->method('exists'); $twitalLoader = new TwitalLoader($mockLoader, null, false); $this->assertFalse($twitalLoader->exists('foo')); }