本文整理汇总了PHP中Twig_Environment::compileSource方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::compileSource方法的具体用法?PHP Twig_Environment::compileSource怎么用?PHP Twig_Environment::compileSource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::compileSource方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testGenerate
/**
* @dataProvider getGenerationTests
*/
public function testGenerate($inputFile, $outputFile)
{
$env = new \Twig_Environment();
$env->addExtension(new \Twig_Extension_Core());
$env->addExtension(new TwigJsExtension());
$env->setLoader(new \Twig_Loader_Filesystem(__DIR__ . '/Fixture/templates'));
$env->setCompiler(new JsCompiler($env));
$source = file_get_contents($inputFile);
$this->assertEquals(file_get_contents($outputFile), $env->compileSource($source, $inputFile));
}
示例2: testExtensionsAreNotInitializedWhenRenderingACompiledTemplate
public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
{
$options = array('cache' => sys_get_temp_dir() . '/twig', 'auto_reload' => false, 'debug' => false);
// force compilation
$twig = new Twig_Environment($loader = new Twig_Loader_Array(array('index' => '{{ foo }}')), $options);
$cache = $twig->getCacheFilename('index');
if (!is_dir(dirname($cache))) {
mkdir(dirname($cache), 0777, true);
}
file_put_contents($cache, $twig->compileSource('{{ foo }}', 'index'));
// check that extensions won't be initialized when rendering a template that is already in the cache
$twig = $this->getMockBuilder('Twig_Environment')->setConstructorArgs(array($loader, $options))->setMethods(array('initExtensions'))->getMock();
$twig->expects($this->never())->method('initExtensions');
// render template
$output = $twig->render('index', array('foo' => 'bar'));
$this->assertEquals('bar', $output);
unlink($cache);
}
示例3: RecursiveDirectoryIterator
require_once __DIR__ . '/../tests/bootstrap.php';
$_SERVER['TWIG_LIB'] = __DIR__ . '/../vendor/twig/twig/lib';
if (!isset($_SERVER['TWIG_LIB'])) {
throw new \RuntimeException('$_SERVER["TWIG_LIB"] must be set.');
}
$env = new Twig_Environment();
$env->setLoader(new Twig_Loader_Filesystem(array(__DIR__ . '/../src-js/templates/twig')));
$env->addExtension(new Twig_Extension_Core());
$handler = new TwigJs\CompileRequestHandler($env, new TwigJs\JsCompiler($env));
foreach (new RecursiveDirectoryIterator(__DIR__ . '/../src-js/templates/twig', RecursiveDirectoryIterator::SKIP_DOTS) as $file) {
if ('.twig' !== substr($file, -5)) {
continue;
}
$request = new TwigJs\CompileRequest(basename($file), file_get_contents($file));
file_put_contents(__DIR__ . '/../src-js/templates/js/' . basename($file, '.twig') . '.js', $handler->process($request));
file_put_contents(__DIR__ . '/../src-js/templates/php/' . basename($file, '.twig') . '.php', $env->compileSource(file_get_contents($file), basename($file)));
}
// port over selected twig integration tests
$testsToPort = array('expressions/array');
$pathToFixtures = realpath($_SERVER['TWIG_LIB'] . '/../test/Twig/Tests/Fixtures');
$targetDir = realpath(__DIR__ . '/../src-js/templates/integration');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathToFixtures, RecursiveDirectoryIterator::SKIP_DOTS)) as $file) {
if ('.test' !== substr($file, -5)) {
continue;
}
$testName = str_replace(DIRECTORY_SEPARATOR, '/', substr($file, strlen($pathToFixtures) + 1, -5));
if (!in_array($testName, $testsToPort, true)) {
continue;
}
// parse test (copy&paste from twig's integrationTest.php)
$test = file_get_contents($file->getRealpath());
示例4: testExtensionsAreNotInitializedWhenRenderingACompiledTemplate
public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
{
$cache = new Twig_Cache_Filesystem($dir = sys_get_temp_dir() . '/twig');
$options = array('cache' => $cache, 'auto_reload' => false, 'debug' => false);
// force compilation
$twig = new Twig_Environment($loader = new Twig_Loader_Array(array('index' => '{{ foo }}')), $options);
$key = $cache->generateKey('index', $twig->getTemplateClass('index'));
$cache->write($key, $twig->compileSource(new Twig_Source('{{ foo }}', 'index')));
// check that extensions won't be initialized when rendering a template that is already in the cache
$twig = $this->getMockBuilder('Twig_Environment')->setConstructorArgs(array($loader, $options))->setMethods(array('initExtensions'))->getMock();
$twig->expects($this->never())->method('initExtensions');
// render template
$output = $twig->render('index', array('foo' => 'bar'));
$this->assertEquals('bar', $output);
Twig_Tests_FilesystemHelper::removeDir($dir);
}
示例5: dumpCompiledTemplate
/**
* Only for debugging, dump the compiled template file to console and die
*
* @param string $template the name of the template in __DIR__/templates/
*/
private function dumpCompiledTemplate($template)
{
$source = file_get_contents(__DIR__ . '/templates/' . $template);
var_dump($this->twig->compileSource($source, $template));
die;
}