1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-10-26 13:16:14 +01:00
Files
php-phpbb/phpBB/includes/template/twig/lexer.php
Nathan Guse eac3c1f75c [feature/twig] BEGIN loops now work
PHPBB3-11598
2013-06-10 11:57:51 -05:00

58 lines
1.5 KiB
PHP

<?php
/**
*
* @package phpBB3
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
class phpbb_template_twig_lexer extends Twig_Lexer
{
public function tokenize($code, $filename = null)
{
$valid_starting_tokens = array(
'BEGIN',
'BEGINELSE',
'END',
'IF',
'ELSE',
'ELSEIF',
'ENDIF',
'DEFINE',
'DEFINE',
'UNDEFINE',
'ENDDEFINE',
/*'INCLUDE',
'INCLUDEPHP',
'INCLUDEJS',*/
'PHP',
'ENDPHP',
'EVENT',
);
// Replace <!-- INCLUDE blah.html --> with {% include 'blah.html' %}
$code = preg_replace('#<!-- INCLUDE(PHP|JS)? (.*?) -->#', "{% INCLUDE$1 '$2' %}", $code);
// Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
// This also strips the $ inside of a tag directly after the token, which was used in <!-- DEFINE $NAME
// This also strips the . inside of a tag directly after the token, which was used in <!-- IF .blah
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
$code = preg_replace('#<!-- (' . implode('|', $valid_starting_tokens) . ') (not )?(\$|\.)?(?:(.*?) ?)?-->#', '{% $1 $2$4 %}', $code);
// Replace all of our variables, {VARNAME} or {$VARNAME}, with Twig style, {{ VARNAME }}
$code = preg_replace('#{\$?([a-zA-Z0-9_\.]+)}#', '{{ $1 }}', $code);
//echo $code;
//exit;
return parent::tokenize($code, $filename);
}
}