Twig:设置 onclick-handler (jsonArray)。语法错误
Twig: Set onclick-handler (jsonArray). Syntax error
如何设置我的 onclick-var 而不会出现语法错误?
{% 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">
感谢您的帮助。
根据 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 %}
我建议你为此制作一个宏,将实现从视图抽象到一个单独的文件中:
{% 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">
如何设置我的 onclick-var 而不会出现语法错误?
{% 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">
感谢您的帮助。
根据 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 %}
我建议你为此制作一个宏,将实现从视图抽象到一个单独的文件中:
{% 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">