当前位置: 首页>>代码示例>>PHP>>正文


PHP Twig_Environment::parse方法代码示例

本文整理汇总了PHP中Twig_Environment::parse方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::parse方法的具体用法?PHP Twig_Environment::parse怎么用?PHP Twig_Environment::parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Twig_Environment的用法示例。


在下文中一共展示了Twig_Environment::parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: testEmptyConstant

 public function testEmptyConstant()
 {
     $stream = $this->environment->parse($this->environment->tokenize('{{ constant("ExtensionTest::SOME_EMPTY_VALUE") }}', 'index'));
     $node = $stream->getNode('body')->getNode(0);
     $this->assertEquals(Twig_Node_Print::class, get_class($node));
     $this->assertEquals(ExtensionTest::SOME_EMPTY_VALUE, $node->getNode('expr')->getAttribute('value'));
 }
开发者ID:silentroach,项目名称:twig-const-resolver,代码行数:7,代码来源:ExtensionEvaluatedTest.php

示例2: validate

 /**
  * @param EmailTemplate                  $value
  * @param Constraint|EmailTemplateSyntax $constraint
  */
 public function validate($value, Constraint $constraint)
 {
     // prepare templates to be validated
     $itemsToValidate = [['field' => 'subject', 'locale' => null, 'template' => $value->getSubject()], ['field' => 'content', 'locale' => null, 'template' => $value->getContent()]];
     $translations = $value->getTranslations();
     foreach ($translations as $trans) {
         if (in_array($trans->getField(), ['subject', 'content'])) {
             $itemsToValidate[] = ['field' => $trans->getField(), 'locale' => $trans->getLocale(), 'template' => $trans->getContent()];
         }
     }
     /** @var \Twig_Extension_Sandbox $sandbox */
     $sandbox = $this->twig->getExtension('sandbox');
     $sandbox->enableSandbox();
     // validate templates' syntax
     $errors = [];
     foreach ($itemsToValidate as &$item) {
         try {
             $this->twig->parse($this->twig->tokenize($item['template']));
         } catch (\Twig_Error_Syntax $e) {
             $errors[] = ['field' => $item['field'], 'locale' => $item['locale'], 'error' => $e->getMessage()];
         }
     }
     $sandbox->disableSandbox();
     // add violations for found errors
     if (!empty($errors)) {
         foreach ($errors as $error) {
             $this->context->addViolation($constraint->message, ['{{ field }}' => $this->getFieldLabel(ClassUtils::getClass($value), $error['field']), '{{ locale }}' => $this->getLocaleName($error['locale']), '{{ error }}' => $error['error']]);
         }
     }
 }
开发者ID:xamin123,项目名称:platform,代码行数:34,代码来源:EmailTemplateSyntaxValidator.php

示例3: getPlaces

 /**
  * {@inheritDoc}
  */
 public function getPlaces($layoutSrc)
 {
     $tokenStream = $this->twig->tokenize($this->twig->getLoader()->getSource($layoutSrc));
     $collector = new PlaceHolderNodeCollector();
     $traverser = new \Twig_NodeTraverser($this->twig, array($collector));
     $traverser->traverse($this->twig->parse($tokenStream));
     return $collector->getCollectedNames();
 }
开发者ID:sitesupra,项目名称:sitesupra,代码行数:11,代码来源:TwigProcessor.php

示例4: visitFile

 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);
     }
 }
开发者ID:goetas,项目名称:twital-bundle,代码行数:8,代码来源:TwitalExtractor.php

示例5: extractTemplate

 protected function extractTemplate($template, MessageCatalogue $catalogue)
 {
     $visitor = $this->twig->getExtension('Symfony\\Bridge\\Twig\\Extension\\TranslationExtension')->getTranslationNodeVisitor();
     $visitor->enable();
     $this->twig->parse($this->twig->tokenize(new \Twig_Source($template, '')));
     foreach ($visitor->getMessages() as $message) {
         $catalogue->set(trim($message[0]), $this->prefix . trim($message[0]), $message[1] ?: $this->defaultDomain);
     }
     $visitor->disable();
 }
开发者ID:yceruto,项目名称:symfony,代码行数:10,代码来源:TwigExtractor.php

示例6: extractTemplate

 protected function extractTemplate($template, MessageCatalogue $catalogue)
 {
     $visitor = $this->twig->getExtension('translator')->getTranslationNodeVisitor();
     $visitor->enable();
     $this->twig->parse($this->twig->tokenize($template));
     foreach ($visitor->getMessages() as $message) {
         $catalogue->set(trim($message[0]), $this->prefix . trim($message[0]), $message[1] ? $message[1] : $this->defaultDomain);
     }
     $visitor->disable();
 }
开发者ID:tejdeeps,项目名称:tejcs.com,代码行数:10,代码来源:TwigExtractor.php

示例7: extract

 /**
  * {@inheritDoc}
  */
 public function extract($directory, MessageCatalogue $catalogue)
 {
     // load any existing translation files
     $finder = new Finder();
     $files = $finder->files()->name('*.twig')->in($directory);
     foreach ($files as $file) {
         $tree = $this->twig->parse($this->twig->tokenize(file_get_contents($file->getPathname())));
         $this->crawlNode($tree, $catalogue);
     }
 }
开发者ID:ratasxy,项目名称:symfony,代码行数:13,代码来源:TwigExtractor.php

示例8: testEscaping

 /**
  * @dataProvider getEscapingTemplates
  */
 public function testEscaping($template, $mustBeEscaped)
 {
     $twig = new \Twig_Environment(null, array('debug' => true, 'cache' => false, 'autoescape' => true, 'optimizations' => 0));
     $twig->addExtension(new RoutingExtension($this->getMock('Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface')));
     $nodes = $twig->parse($twig->tokenize($template));
     $this->assertSame($mustBeEscaped, $nodes->getNode('body')->getNode(0)->getNode('expr') instanceof \Twig_Node_Expression_Filter);
 }
开发者ID:snailwork,项目名称:rayjack,代码行数:10,代码来源:RoutingExtensionTest.php

示例9: 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 . ')');
     }
 }
开发者ID:Broncko,项目名称:Savant,代码行数:32,代码来源:integrationTest.php

示例10: testEscaping

 /**
  * Tests the escaping
  *
  * @dataProvider providerTestEscaping
  */
 public function testEscaping($template, $expected)
 {
     $twig = new \Twig_Environment(NULL, array('debug' => TRUE, 'cache' => FALSE, 'autoescape' => TRUE, 'optimizations' => 0));
     $twig->addExtension((new TwigExtension())->setGenerators($this->getMock('Drupal\\Core\\Routing\\UrlGeneratorInterface')));
     $nodes = $twig->parse($twig->tokenize($template));
     $this->assertSame($expected, $nodes->getNode('body')->getNode(0)->getNode('expr') instanceof \Twig_Node_Expression_Filter);
 }
开发者ID:Nikola-xiii,项目名称:d8intranet,代码行数:12,代码来源:TwigExtensionTest.php

示例11: testCompile

 /**
  * @dataProvider fixtureProvider
  */
 public function testCompile($source, $expected)
 {
     $twig = new \Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), ['cache' => false, 'autoescape' => false, 'optimizations' => 0]);
     $twig->addExtension(new ElixirExtension('public', 'build'));
     $nodes = $twig->parse($twig->tokenize($source));
     $this->assertEquals($expected, $nodes->getNode('body')->getNode(0));
 }
开发者ID:brieucthomas,项目名称:elixir-twig-extension,代码行数:10,代码来源:ElixirExtensionTest.php

示例12: 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 . ')');
     }
 }
开发者ID:BozzaCoon,项目名称:SPHERE-Framework,代码行数:59,代码来源:IntegrationTestCase.php

示例13: testForOptimizer

 /**
  * @dataProvider getTestsForForOptimizer
  */
 public function testForOptimizer($template, $expected)
 {
     $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false));
     $stream = $env->parse($env->tokenize($template, 'index'));
     foreach ($expected as $target => $withLoop) {
         $this->assertTrue($this->checkForConfiguration($stream, $target, $withLoop), sprintf('variable %s is %soptimized', $target, $withLoop ? 'not ' : ''));
     }
 }
开发者ID:d-f-d,项目名称:100gramm,代码行数:11,代码来源:OptimizerTest.php

示例14: parse

 protected final function parse($file, $debug = false)
 {
     $content = file_get_contents(__DIR__ . '/Fixture/' . $file);
     $env = new \Twig_Environment();
     $env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
     $env->addExtension(new TranslationExtension($translator, $debug));
     $env->setLoader(new \Twig_Loader_String());
     return $env->parse($env->tokenize($content));
 }
开发者ID:pixel-cookers,项目名称:JMSTranslationBundle,代码行数:9,代码来源:BaseTwigTestCase.php

示例15: isValid

 /**
  * {@inheritdoc}
  */
 public function isValid($template)
 {
     try {
         $this->twig->parse($this->twig->tokenize($template));
         return true;
     } catch (\Twig_Error_Syntax $e) {
         return false;
     }
 }
开发者ID:narno,项目名称:phpoole-library,代码行数:12,代码来源:Twig.php


注:本文中的Twig_Environment::parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。