Twig,将变量与字符串进行比较

时间:2013-12-09 15:40:10

标签: php string twig

当我需要将变量与字符串进行比较时,我在twig中有一个非常简单的模板有问题。

模板

{% if test_var != "" %}
  Test 42
{% endif %}

PHP代码

require_once REP_GLOBAL."/vendor/autoload.php";
$loader = new Twig_Loader_Filesystem(array(REP_GLOBAL."/ressources/twig/emails"));
$twig = new Twig_Environment($loader);
$template = $twig->loadTemplate('maj_coord_perso.html');
$msg = $template->render(array('test_var' => "TEST"));
echo $msg;

错误

PHP Fatal error:  
Uncaught exception 'Twig_Error_Syntax' with message 'Unexpected character "\\" 
in "maj_coord_perso.html" at line 1' in ***/vendor/twig/twig/lib/Twig/Lexer.php:282\n
Stack trace:\n
#0 ***/vendor/twig/twig/lib/Twig/Lexer.php(203): Twig_Lexer->lexExpression()\n
#1 ***/vendor/twig/twig/lib/Twig/Lexer.php(109): Twig_Lexer->lexBlock()\n
#2 ***/vendor/twig/twig/lib/Twig/Environment.php(469): Twig_Lexer->tokenize('{% if address.w...', 'maj_coord_perso...')\n
#3 ***/vendor/twig/twig/lib/Twig/Environment.php(559): Twig_Environment->tokenize('{% if address.w...', 'maj_coord_perso...')\n
#4 ***/vendor/twig/twig/lib/Twig/Environment.php(331): Twig_Environment->compileSource('{% if address.w...', 'maj_coord_perso...')\n

模板文件的编码是ISO-8859-1,但我对UTF-8编码模板有同样的错误。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

set_magic_quotes_runtime(0);