2017-04-16 53 views
0

我有一個基地樹枝模板在那裏我可以插入身體和JS按照我need.Base模板就像下面:的JavaScript在樹枝模板佈局產生錯誤

<!DOCTYPE html> 
<html> 
<head> 
    <title>{{ title }}</title> 
    {{ block("stylesheets") }} 
</head> 
<body> 
<div class="container"> 
    <div align="center"> 

     {{ block("body") }} 

    </div> 
</div> 
{{ block("javascripts") }} 

</body> 
</html> 

,我有一個welcome.html這將使用此template.Welcome頁面有一些HTML和javascript.It呈現HTML裏面如果沒有JS是provided.If我提供任何JavaScript它停止工作,我得到以下錯誤:

Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'A block must start with a tag name.' in C:\xampp\htdocs\practice\PHPMVC\Elixir\View\Welcome\welcome.html:14 Stack trace: #0 C:\xampp\htdocs\practice\PHPMVC\vendor\twig\twig\lib\Twig\TokenParser\Block.php(40): Twig_Parser->subparse(Array, true) #1 C:\xampp\htdocs\practice\PHPMVC\vendor\twig\twig\lib\Twig\Parser.php(190): Twig_TokenParser_Block->parse(Object(Twig_Token)) #2 C:\xampp\htdocs\practice\PHPMVC\vendor\twig\twig\lib\Twig\Parser.php(103): Twig_Parser->subparse(NULL, false) #3 C:\xampp\htdocs\practice\PHPMVC\vendor\twig\twig\lib\Twig\Environment.php(692): Twig_Parser->parse(Object(Twig_TokenStream)) #4 C:\xampp\htdocs\practice\PHPMVC\vendor\twig\twig\lib\Twig\Environment.php(750): Twig_Environment->parse(Object(Twig_TokenStream)) #5 C:\xampp\htdocs\practice\PHPMVC\vendor\twig\twig\lib\Twig\Environment.php(447): Twig_Environment->compileSource(Object(Twig_Source)) #6 C:\xampp\htdocs\practice\PHPMVC\vendor\twig\twig\lib\Twig\Environment.php(362): Twig_E in C:\xampp\htdocs\practice\PHPMVC\Elixir\View\Welcome\welcome.html on line 14

Welcome.html:

{ extends "Base/Base.html" } 
{ block body } 
<ul> 
{ for user in users } 
    <li>{{ user.name}} is {{ user.age }} years old !</li> 
{ endfor } 
</ul> 
<input type = 'button' value = 'mybtn' id = 'btn'> 

{ endblock body } 

{ block javascripts } 
<script type="text/javascript"> 
document.getElementById('btn').addEventListener('click',function(){ 

}) 

</script> 
{ endblock javascripts } 

我已經使用詞法重寫語法規則類似以下內容:

$lexer = new \Twig_Lexer($twig,array(
         'tag_block' =>array('{','}'), 
         'tag_variable' =>array('{{','}}') 
)); 

回答

2

您lexxer重寫將匹配您的js的樹枝標籤塊

是JS對象的括號:

function(){} 

你可以使用varbatim標籤

{ block javascripts } 
{ verbatim } 
<script type="text/javascript"> 
document.getElementById('btn').addEventListener('click',function(){ 

}) 

</script> 
{ endverbatim } 
{ endblock javascripts } 

verbatim之間的內容將不會被樹枝

https://twig.sensiolabs.org/doc/2.x/tags/verbatim.html

+0

何不錯的解析!做得好! – scoolnico

+0

..格雷特! :D ................. –

+0

非常歡迎,很高興幫助;-) –