2015-07-13 62 views
1

如何設置我的onclick-var沒有語法錯誤?Twig:設置onclick-handler(jsonArray)。語法錯誤

{% if active is defined and active == false %} 
    {% set onclick = 'javascript:void(0); false;' %} 
{% else %} 
    {% set onclick = "myApp.dialog.show({ 
      'title': '{{ 'app.title'|trans({}, 'MyBundle') }}', 
      'width': '{{ dialog.width }}', 
      'height': '{{ dialog.height }}' 
     });" 
    %} 
{% endif %} 
<button onclick="{{ onclick }}" type="button" class="btn btn-default" aria-label="Left Align"> 

感謝您的幫助。

回答

0

按照documentation over here可以使用set標籤捕獲數據:

{% if active is defined and active == false %} 
    {% set onclick = 'javascript:void(0); false;' %} 
{% else %} 
    {% set onclick %} 
     myApp.dialog.show({ 
      'title': '{{ 'app.title'|trans({}, 'MyBundle') }}', 
      'width': '{{ dialog.width }}', 
      'height': '{{ dialog.height }}' 
     }); 
    {% endset %} 
{% endif %} 
+1

謝謝!它工作正常... – gondor

0

我建議你做的這個宏,抽象掉的實施從視圖到一個單獨的文件:

{% macro clickHandler(active) %} 
    {% if active is defined and active == false %} 
     javascript:void(0); false; 
    {% else %} 
     myApp.dialog.show({ 
      'title': '{{ 'app.title'|trans({}, 'MyBundle') }}', 
      'width': '{{ dialog.width }}', 
      'height': '{{ dialog.height }}' 
     }); 
    {% endif %} 
{% endmacro %} 

是否會讓:

{% import "macros.html" as macros %} 
<button onclick="{{ macros.clickHandler(activeState) }}" type="button" class="btn btn-default" aria-label="Left Align"> 

http://twig.sensiolabs.org/doc/tags/macro.html