I've got a tag that looks like this:
我有一个看起来像这样的标签:
{% partial "partials/vehicleform.html" vehicle=vehicles.empty_form %}
Which just renders an empty form. But now I want to pass the output of that to the escapejs
filter so that I can use it in a JavaScript variable. How can I do that?
这只是呈现一个空表格。但是现在我想将它的输出传递给escapejs过滤器,以便我可以在JavaScript变量中使用它。我怎样才能做到这一点?
15
Many tags support as variablename
-- that is, simple put as variablename
at the end of the tag and then the output of that tag is placed in the variable rather than displayed.
许多标记支持作为variablename - 也就是说,在标记的末尾简单地将其作为variablename放置,然后将该标记的输出放在变量中而不是显示。
This {% partial %}
tag may support that. Here's an example, if it does:
此{%partial%}标记可能支持该标记。这是一个例子,如果是这样的话:
{% partial "partials/vehicleform.html" vehicle=vehicles.empty_form as myvar %}{{ myvar|escapejs }}
If the tag in question is the "Partial tag" snippet then it appears it doesn't support this. But it probably could be rewritten to support it.
如果相关标签是“部分代码”代码段,那么它似乎不支持此功能。但它可能会被重写以支持它。
You could use the "Capture template output as a variable" snippet, and then apply the filter to the captured content, like so:
您可以使用“捕获模板输出作为变量”片段,然后将过滤器应用于捕获的内容,如下所示:
{% captureas myvar %}{% partial "partials/vehicleform.html" vehicle=vehicles.empty_form %}{% endcaptureas %}{{ myvar|escapejs }}
1
Another solution to get the data into a JS variable:
将数据转换为JS变量的另一种解决方案:
<div class="display:none" id="empty-vehicle-form">{% partial "partials/vehicleform.html" vehicle=vehicles.empty_form %}</div>
Then nab it and remove it at the same time
然后抓住它并同时将其移除
var empty_form = $('#empty-vehicle-form').remove().html();
The advantage of this solution is that your other JS scripts can preprocess it before you rip it out of the DOM. escapejs
also creates bigger filesizes with all those escape chars.
此解决方案的优点是您的其他JS脚本可以在将其从DOM中删除之前对其进行预处理。 escapejs还会使用所有这些转义字符创建更大的文件大小。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/08/10/5658c9f150d18ef11a36bbdc6ce4e430.html。