Javascript 中的变量 Twig
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19164995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
variable Twig in Javascript
提问by Barno
How can i pass {{app.user}} into Javascript ?
我如何将 {{app.user}} 传递到 Javascript 中?
for now I do a block like;
现在我做了一个块;
<script type="text/javascript">
var app_name = '{{ app_name }}';
var app_url= '{{ app_url }}';
var app_description= '{{ app_description }}';
var app_email= '{{ app_email }}';
var app_title= '{{ app_title }}';
var app_dominio= '{{ app_dominio }}';
var env = '{{ app.environment }}';
</script>
where these parameters are set in config.yml
这些参数在 config.yml 中设置
回答by zapcost
I don't understand what's exactly your problem with the solution you choose, it should work well with {{ app.user }} except that app.user is an object, so you should have a toArray function into your user and call :
我不明白你选择的解决方案到底是什么问题,它应该适用于 {{ app.user }} ,除了 app.user 是一个对象,所以你应该有一个 toArray 函数进入你的用户并调用:
app_user = {{ app.user.toArray|json_encode() }};
Or call each parameter of the user like {{ app.user.id }}
或者像 {{ app.user.id }} 这样调用用户的每个参数
Documentation : https://twig.sensiolabs.org/doc/filters/json_encode.html
文档:https: //twig.sensiolabs.org/doc/filters/json_encode.html
You should use json_encode for your variables above, if you have a quote into one of your string it will break your javascript.
您应该对上面的变量使用 json_encode,如果您在其中一个字符串中有引号,它会破坏您的 javascript。
Example for profile:
个人资料示例:
<script type="text/javascript">
nickname = {{ profile.nickname|json_encode() }}; // Nickname will be a string
// 2nd solution if you have more informations related to profile
profile = {
nickname: {{ profile.nickname|json_encode() }},
lastname: {{ profile.lastname|json_encode() }}
};
// Profile is now an object with a nickname property.
// use profile.nickname on your javascripts
</script>
回答by Lulhum
The accepted solution doesn't work (anymore ?) because of twig autoescaping the outputs, changing all the JSON "
with "
;.
接受的解决方案不起作用(不再起作用?),因为树枝自动转义输出,"
用"
;更改所有 JSON 。
Equivalent would now have to use the raw
filter:
等效项现在必须使用raw
过滤器:
<script type="text/javascript">
nickname = {{ profile.nickname|json_encode()|raw }}; // Nickname will be a string
// 2nd solution if you have more informations related to profile
profile = {
nickname: {{ profile.nickname|json_encode()|raw }},
lastname: {{ profile.lastname|json_encode()|raw }}
};
// Profile is now an object with a nickname property.
// use profile.nickname on your javascripts
</script>
That being said, directly printing the raw JSON into the javascript may cause some problems, has in this configuration:
话虽如此,直接将原始 JSON 打印到 javascript 中可能会导致一些问题,在此配置中有:
<script>
var myvar = {{ '{"test": "</script>"}'|raw }};
</script>
The </script>
tag in the JSON would be interpreted by the HTML parser, resulting in a broken script.
</script>
JSON 中的标记将由 HTML 解析器解释,从而导致脚本损坏。
The truly correct way to do this would rather be to print the JSON as an escaped string, and then parse it within the js script:
真正正确的方法是将 JSON 打印为转义字符串,然后在 js 脚本中解析它:
<script>
var myvar = JSON.parse('{{ '{"test": "</script>"}'|e('js') }}');
</script>
回答by AV Jaworski
Try this:
试试这个:
var title = '{{ 'My Daily Activities'|trans({}, 'general') }}';
with additional apostrophy
附加撇号
回答by jink
Use an attribute or any tag
使用属性或任何标签
example: <span profile="{{ profile.nickname }}"></span>
例子: <span profile="{{ profile.nickname }}"></span>