SyntaxError:在twig文件中显示数字文字后立即启动标识符

时间:2013-11-03 12:23:22

标签: symfony twig

{% block reviewresult %}
<div class="scrollerList"> 
</div>

{% endblock %}
{% block myJavascript %}
<script>
    var stateObject = {};
    var title = "Single";
    var urlEncode = "'" +{{ tag|url_encode(true) }} + "'";
    alert(urlEncode);

    var newUrl = baseUrl + "/taglist/" + urlEncode.toString();

    $(document).ready(function() {
        ShuffleObj.changeUrl(stateObject, title, newUrl);

        ffleObj.setScrollUrl(baseUrl + '/taglist/playlist/' + urlEncode.toString());
    });
</script>
{% endblock %}

var urlEncode = "'" +{{ tag|url_encode(true) }} + "'";中的上述代码显示错误 SyntaxError:标识符在数字文字后立即开始。在此问题中,代码值为tag= 123 Forever;

1 个答案:

答案 0 :(得分:2)

此:

var urlEncode = "'" +{{ tag|url_encode(true) }} + "'";

将生成以下代码:

var urlEncode = "'" + someTag + "'";

并且js将someTag解释为标识符。所以,试试这个:

var urlEncode = ' {{ tag|url_encode(true) }} ';