本文整理汇总了PHP中Twig_Environment::compile方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::compile方法的具体用法?PHP Twig_Environment::compile怎么用?PHP Twig_Environment::compile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::compile方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testIntegration
/**
* @dataProvider getTests
*/
public function testIntegration($file, $test, $message, $templates)
{
$loader = new Twig_Loader_Array($templates);
$twig = new Twig_Environment($loader, array('trim_blocks' => true, 'cache' => false));
$twig->addExtension(new Twig_Extension_Escaper());
$twig->addExtension(new TestExtension());
try {
$template = $twig->loadTemplate('index.twig');
} catch (Twig_SyntaxError $e) {
$e->setFilename($file);
throw $e;
} catch (Exception $e) {
throw new Twig_Error($e->getMessage() . ' (in ' . $file . ')');
}
preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\\-\\-DATA\\-\\-|$)/s', $test, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$output = trim($template->render(eval($match[1] . ';')), "\n ");
$expected = trim($match[2], "\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 . ')');
}
}
示例2: checkNode
/**
* Extracts formulae from filter function nodes.
*
* @return array|null The formula
*/
private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if ($node instanceof \Twig_Node_Expression_Function) {
$name = $node->getNode('name')->getAttribute('name');
if ($env->getFunction($name) instanceof AsseticFilterFunction) {
$arguments = array();
foreach ($node->getNode('arguments') as $argument) {
$arguments[] = eval('return '.$env->compile($argument).';');
}
$invoker = $env->getExtension('assetic')->getFilterInvoker($name);
$factory = $invoker->getFactory();
$inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
$filters = $invoker->getFilters();
$options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());
if (!isset($options['name'])) {
$options['name'] = $factory->generateAssetName($inputs, $filters);
}
return array($inputs, $filters, $options);
}
}
}
示例3: doIntegrationTest
protected function doIntegrationTest($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->addGlobal('global', 'global');
foreach ($this->getExtensions() as $extension) {
$twig->addExtension($extension);
}
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: testTrans
/**
* @dataProvider getTransTests
*/
public function testTrans($template, $expected, array $variables = array())
{
if ($expected != $this->getTemplate($template)->render($variables)) {
print $template . "\n";
$loader = new \Twig_Loader_Array(array('index' => $template));
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index'))) . "\n\n";
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
}
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
}
示例5: testIntegration
/**
* @dataProvider getTests
*/
public function testIntegration($file, $message, $templates, $exception, $outputs)
{
$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 Twig_Extension_Escaper());
$twig->addExtension(new TestExtension());
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($e->getMessage() . ' (in ' . $file . ')');
}
try {
$output = trim($template->render(eval($match[1] . ';')), "\n ");
} catch (Exception $e) {
$output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
}
$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 . ')');
}
}
示例6: testIntegration
public function testIntegration()
{
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::$fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
if (!preg_match('/\\.test$/', $file)) {
continue;
}
$test = file_get_contents($file->getRealpath());
if (!preg_match('/--TEST--\\s*(.*?)\\s*((?:--TEMPLATE(?:\\(.*?\\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace(self::$fixturesDir . '/', '', $file)));
}
$message = $match[1];
$templates = array();
preg_match_all('/--TEMPLATE(?:\\((.*?)\\))?--(.*?)(?=\\-\\-TEMPLATE|$)/s', $match[2], $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$templates[$match[1] ? $match[1] : 'index.twig'] = $match[2];
}
$loader = new Twig_Loader_Array($templates);
$twig = new Twig_Environment($loader, array('trim_blocks' => true, 'cache' => false));
$twig->addExtension(new Twig_Extension_Escaper());
$twig->addExtension(new TestExtension());
$template = $twig->loadTemplate('index.twig');
preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\\-\\-DATA\\-\\-|$)/s', $test, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$output = trim($template->render(eval($match[1] . ';')), "\n ");
$expected = trim($match[2], "\n ");
$this->assertEquals($expected, $output, $message . ' (in ' . str_replace(self::$fixturesDir, '', $file) . ')');
if ($output != $expected) {
echo 'Compiled template that failed:';
foreach (array_keys($templates) as $name) {
$source = $loader->getSource($name);
echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
}
}
}
}
}
示例7: validate
private function validate(\Twig_Environment $twig, $template, $file)
{
$realLoader = $twig->getLoader();
try {
$temporaryLoader = new \Twig_Loader_Array(array((string) $file => $template));
$twig->setLoader($temporaryLoader);
$nodeTree = $twig->parse($twig->tokenize($template, (string) $file));
$twig->compile($nodeTree);
$twig->setLoader($realLoader);
} catch (\Twig_Error $e) {
$twig->setLoader($realLoader);
return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e);
}
return array('template' => $template, 'file' => $file, 'valid' => true);
}
示例8: InvalidArgumentException
continue;
}
$test = file_get_contents($file->getRealpath());
if (!preg_match('/--TEST--\\s*(.*?)\\s*((?:--TEMPLATE(?:\\(.*?\\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir . '/', '', $file)));
}
$prefix = rand(1, 999999999);
$message = $match[1];
$templates = array();
preg_match_all('/--TEMPLATE(?:\\((.*?)\\))?--(.*?)(?=\\-\\-TEMPLATE|$)/s', $match[2], $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$templates[$prefix . '_' . ($match[1] ? $match[1] : 'index.twig')] = $match[2];
}
$loader = new Twig_Loader_Var($templates, $prefix);
$twig = new Twig_Environment($loader, array('trim_blocks' => true));
$twig->addExtension(new Twig_Extension_Escaper());
$template = $twig->loadTemplate($prefix . '_index.twig');
preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\\-\\-DATA\\-\\-|$)/s', $test, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$output = trim($template->render(eval($match[1] . ';')), "\n ");
$expected = trim($match[2], "\n ");
$t->is($output, $expected, $message);
if ($output != $expected) {
$t->comment('Compiled template that failed:');
foreach (array_keys($templates) as $name) {
list($source, ) = $loader->getSource($name);
$t->comment($twig->compile($twig->parse($twig->tokenize($source))));
}
}
}
}