setLexer() public method

Sets the Lexer instance.
public setLexer ( Twig_LexerInterface $lexer )
$lexer Twig_LexerInterface
Example #1
0
 /**
  * Spawns a new instance of Twig.
  *
  * @return object
  **/
 protected function spawn()
 {
     // register the Twig autoloader.
     Twig_Autoloader::register();
     // Init the Twig loader.
     $loader = new Twig_Loader_String();
     // check if the cache dir is set.
     if ($this->compile_dir) {
         if (is_null($this->debug)) {
             $this->debug = is_dev_mode();
         }
         $twig = new Twig_Environment($loader, array('autoescape' => FALSE, 'cache' => $this->compile_dir, 'debug' => $this->debug, 'charset' => $this->charset, 'base_template_class' => $this->base_template_class, 'strict_variables' => $this->strict_variables, 'autoescape' => $this->autoescape, 'optimizations' => $this->optimizations));
     } else {
         $twig = new Twig_Environment($loader, array('autoescape' => FALSE));
     }
     // init all functions as Twig functions.
     foreach ($this->allowed_functions as $function) {
         $twig->addFunction($function, new Twig_Function_Function($function));
     }
     // setup debugger
     $twig->addExtension(new Twig_Extension_Debug());
     // setup the Lexer
     $lexer = new Twig_Lexer($twig, $this->delimiters);
     $twig->setLexer($lexer);
     // finally, return the object.
     return $twig;
 }
Example #2
0
 public function __construct($data = array())
 {
     parent::__construct($data);
     $dirs = Zend_Registry::get('dirs');
     $template = Zend_Registry::get('theme');
     $config = Zend_Registry::get('config');
     $qool_module = Zend_Registry::get('Qool_Module');
     // Class Constructor.
     // These automatically get set with each new instance.
     $loader = new Twig_Loader_Filesystem(APPL_PATH . $dirs['structure']['templates'] . DIR_SEP . $qool_module . DIR_SEP . $template . DIR_SEP);
     $twig = new Twig_Environment($loader, array('cache' => APPL_PATH . $dirs['structure']['cache'] . DIR_SEP . 'twig' . DIR_SEP));
     $lexer = new Twig_Lexer($twig, array('tag_comment' => array('<#', '#>}'), 'tag_block' => array('<%', '%>'), 'tag_variable' => array('<<', '>>')));
     $twig->setLexer($lexer);
     include_once APPL_PATH . $dirs['structure']['lib'] . DIR_SEP . 'Qool' . DIR_SEP . 'Template' . DIR_SEP . 'template.php';
     if (file_exists(APPL_PATH . $dirs['structure']['templates'] . DIR_SEP . $qool_module . DIR_SEP . $template . DIR_SEP . 'functions.php')) {
         include_once APPL_PATH . $dirs['structure']['templates'] . DIR_SEP . $qool_module . DIR_SEP . $template . DIR_SEP . 'functions.php';
     }
     $funcs = get_defined_functions();
     foreach ($funcs['user'] as $k => $v) {
         $twig->addFunction($v, new Twig_Function_Function($v));
     }
     $this->_twig = $twig;
     $this->assign('config', $config);
     Zend_Registry::set('tplExt', 'html');
 }
Example #3
0
 /**
  * @dataProvider getData
  */
 public function testExpressions($expression, $expectedViolation)
 {
     $twig = new \Twig_Environment();
     $twig->setLexer(new Lexer($twig));
     $validator = new Validator();
     $violations = $validator->validate(new Official(), $twig->tokenize($expression));
     if ($expectedViolation) {
         $this->assertSame(1, count($violations));
         $this->assertSame($expectedViolation, $violations[0]->getReason());
     } else {
         $this->assertSame(0, count($violations));
     }
 }
 /**
  * Constructor
  *
  * @param array $config An array of default values.
  */
 public function __construct($config)
 {
     $this->config = $config;
     ShortcodesTrait::setShortcodesClass($this);
     // Set up Twig environment
     $this->loader = new \Twig_Loader_Array([]);
     $this->twig = new Twig\Environment($this->loader, ['use_strict_variables' => false]);
     // Set up sandbox for parsing shortcodes
     $this->policy = new \Twig_Sandbox_SecurityPolicy();
     $this->twig->addExtension(new \Twig_Extension_Sandbox($this->policy, true));
     $this->policy->setAllowedTags($this->loadShortcodes());
     // Modify lexer to match special shortcode syntax
     $lexer = new \Twig_Lexer($this->twig, array('tag_comment' => ['{#', '#}'], 'tag_block' => ['{{%', '%}}'], 'tag_variable' => ['{#', '#}'], 'interpolation' => ['#{', '}']));
     $this->twig->setLexer($lexer);
 }
Example #5
0
 /**
  * Constructor.
  *
  * @param \src\path_helper $path_helper
  * @param \src\config\config $config
  * @param \src\user $user
  * @param \src\template\context $context template context
  * @param \src\extension\manager $extension_manager extension manager, if null then template events will not be invoked
  */
 public function __construct(\src\path_helper $path_helper, $config, $user, \src\template\context $context, \src\extension\manager $extension_manager = null)
 {
     $this->path_helper = $path_helper;
     $this->src_root_path = $path_helper->get_src_root_path();
     $this->php_ext = $path_helper->get_php_ext();
     $this->config = $config;
     $this->user = $user;
     $this->context = $context;
     $this->extension_manager = $extension_manager;
     $this->cachepath = $this->src_root_path . 'cache/twig/';
     // Initiate the loader, __main__ namespace paths will be setup later in set_style_names()
     $loader = new \src\template\twig\loader('');
     $this->twig = new \src\template\twig\environment($this->config, $this->path_helper, $this->extension_manager, $loader, array('cache' => defined('IN_INSTALL') ? false : $this->cachepath, 'debug' => defined('DEBUG'), 'auto_reload' => (bool) $this->config['load_tplcompile'], 'autoescape' => false));
     $this->twig->addExtension(new \src\template\twig\extension($this->context, $this->user));
     if (defined('DEBUG')) {
         $this->twig->addExtension(new \Twig_Extension_Debug());
     }
     $lexer = new \src\template\twig\lexer($this->twig);
     $this->twig->setLexer($lexer);
     // Add admin namespace
     if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->src_root_path . $this->path_helper->get_adm_relative_path() . 'style/')) {
         $this->twig->getLoader()->setPaths($this->src_root_path . $this->path_helper->get_adm_relative_path() . 'style/', 'admin');
     }
 }
Example #6
0
 public function __construct()
 {
     $this['reporter.console'] = function () {
         return new ConsoleReporter();
     };
     $this['reporter.checkstyle'] = function () {
         return new CheckstyleReporter();
     };
     $this['twig'] = function ($container) {
         $twig = new \Twig_Environment();
         $twig->setLexer(new Lexer($twig));
         return $twig;
     };
     $this['validator'] = function () {
         return new Validator();
     };
 }
Example #7
0
/**
 * Avisota newsletter and mailing system
 * Copyright © 2016 Sven Baumann
 *
 * PHP version 5
 *
 * @copyright  way.vision 2016
 * @author     Sven Baumann <*****@*****.**>
 * @package    avisota/contao-message
 * @license    LGPL-3.0+
 * @filesource
 */
/** @var \Pimple $container */
/**
 * Define message renderer
 */
$container['avisota.message.renderer'] = $container->share(function () {
    return new \Avisota\Contao\Message\Core\Renderer\MessageRenderer();
});
$container['avisota.message.tagReplacementEngine'] = $container->share(function () {
    $debug = $GLOBALS['TL_CONFIG']['debugMode'] || $GLOBALS['TL_CONFIG']['twigDebugMode'];
    $loader = new \Twig_Loader_Array(array());
    $twig = new \Twig_Environment($loader, array('autoescape' => false, 'debug' => $debug));
    // Add debug extension
    if ($debug || $GLOBALS['TL_CONFIG']['twigDebugExtension']) {
        $twig->addExtension(new Twig_Extension_Debug());
    }
    $lexer = new Twig_Lexer($twig, array('tag_comment' => array('{#', '#}'), 'tag_block' => array('{%', '%}'), 'tag_variable' => array('##', '##'), 'interpolation' => array('#{', '}')));
    $twig->setLexer($lexer);
    return new \Avisota\Contao\Message\Core\Renderer\TagReplacementService($twig);
});
 /**
  * Render view
  *
  * @param $view string The name of the view
  * @param $data array  The data to use in rendering in the view
  */
 public function render($view, $data = array())
 {
     // Retrieve data necessary for properly rendering header and footer, and
     //   add that data to the template data
     $areaAcc = new \TMT\accessor\AreaAccessor();
     $employeeAcc = new \TMT\accessor\Employee();
     $linkAcc = new \TMT\accessor\Links();
     // Determine if user is admin or superuser
     $admin = $this->isAdmin();
     $su = $this->isSuperuser();
     // Get user and area information
     $user = $employeeAcc->get($this->user['netId']);
     $areaArray = $areaAcc->getAll($this->user['netId']);
     $areas = array();
     if (isset($this->user['area'])) {
         foreach ($areaArray as $area) {
             $areas[] = array('id' => $area->ID, 'name' => $area->longName);
         }
         // Retrieve link tree
         $links = $linkAcc->getTree($this->user['area']);
         $this->cleanLinkTree($links, $admin, $su);
     }
     // Check environment
     $environment = $this->getEnvironment();
     // Get quicklinks
     $quicklinks = $this->getAccessor("Quicklinks")->getByUser($this->user['netId']);
     $notificationsUrl = getenv("NOTIFICATIONSURL");
     // Add data necessary for the main header and footer to load properly
     $data['templateData'] = array("area" => isset($this->user['area']) ? $this->user['area'] : null, "areaName" => isset($this->user['area']) ? $areaAcc->get($this->user['area'])->longName : null, "areaGuid" => isset($this->user['areaGuid']) ? $this->user['areaGuid'] : null, "areas" => $areas, "authenticated" => $this->authenticated, "canSU" => $this->canBeSuperuser(), "environment" => $environment, "firstName" => $user->firstName, "isSU" => $su, "jwt" => $this->createJWT(), "lastName" => $user->lastName, "links" => isset($links) ? $links : null, "netId" => $this->user['netId'], "notificationsUrl" => $notificationsUrl, "quicklinks" => $quicklinks, "server" => $_SERVER['SERVER_NAME']);
     // load twig
     $twigLoader = new \Twig_Loader_Filesystem(self::VIEWS_PATH);
     $twig = new \Twig_Environment($twigLoader);
     // to avoid conflicts with angularjs use of {{ }}
     $lexer = new \Twig_Lexer($twig, array('tag_comment' => array('[#', '#]'), 'tag_block' => array('[%', '%]'), 'tag_variable' => array('[[', ']]'), 'interpolation' => array('#[', ']')));
     $twig->setLexer($lexer);
     // render a view
     echo $twig->render($view . self::VIEW_FILE_TYPE, $data);
 }
 /**
  * Render a twig file with custom twig tags.
  *
  * @param string $template
  * @param array  $parameters
  * @param string $sourceDir
  *
  * @return string
  */
 public function renderTwig($template, array $parameters, $sourceDir)
 {
     $twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array($sourceDir)), array('debug' => true, 'cache' => false, 'strict_variables' => true, 'autoescape' => false));
     // Ruby erb template syntax
     $lexer = new \Twig_Lexer($twig, array('tag_comment' => array('<%#', '%>'), 'tag_block' => array('<%', '%>'), 'tag_variable' => array('<%=', '%>')));
     $twig->setLexer($lexer);
     return $twig->render($template, $parameters);
 }
 /**
  * Gets the 'sonata.formatter.twig.env.text' service.
  *
  * This service is shared.
  * This method always returns the same instance of the service.
  *
  * This service is private.
  * If you want to be able to request this service from the container directly,
  * make it public, otherwise you might end up with broken code.
  *
  * @return \Twig_Environment A Twig_Environment instance.
  */
 protected function getSonata_Formatter_Twig_Env_TextService()
 {
     $this->services['sonata.formatter.twig.env.text'] = $instance = new \Twig_Environment(new \Sonata\FormatterBundle\Twig\Loader\LoaderSelector(new \Twig_Loader_String(), $this->get('twig.loader')), array('debug' => false, 'strict_variables' => false, 'charset' => 'UTF-8'));
     $instance->addExtension(new \Twig_Extension_Sandbox(new \Sonata\FormatterBundle\Twig\SecurityPolicyContainerAware($this, array(0 => 'sonata.formatter.twig.control_flow', 1 => 'sonata.formatter.twig.gist', 2 => 'sonata.media.formatter.twig')), true));
     $instance->addExtension($this->get('sonata.formatter.twig.control_flow'));
     $instance->addExtension($this->get('sonata.formatter.twig.gist'));
     $instance->addExtension($this->get('sonata.media.formatter.twig'));
     $instance->setLexer(new \Twig_Lexer($instance, array('tag_comment' => array(0 => '<#', 1 => '#>'), 'tag_block' => array(0 => '<%', 1 => '%>'), 'tag_variable' => array(0 => '<%=', 1 => '%>'))));
     return $instance;
 }