简体   繁体   English

如何在Jtwig / Twig中打印名称中带有空格的变量?

[英]How to print a variable that has a whitespace in its name in Jtwig/Twig?

Let's say I have a model in JSON: 假设我有一个JSON模型:

{"Id": 123, "Full Name": "Foo Bar"}

I can output the value of Id in Jtwig like so: {{ Id }} 我可以像这样在Jtwig中输出Id的值: {{ Id }}

But how do I output the value of Full Name ? 但是,如何输出全名的值?

So after reading the official docs and very few posts around the Internet that are vaguely related I've figured out there is no way to do it without the changes in the Jtwig core . 因此,在阅读了官方文档和互联网上几乎没有关联的帖子后,我发现如果没有Jtwig核心的改变,那就没有办法了。 The only solution would be to modify the input model for the template by embedding it in a variable that has a valid identifier name, for example: 唯一的解决方案是通过将模板嵌入具有有效标识符名称的变量来修改模板的输入模型,例如:

{"data": {"Id": 123, "Full Name": "Foo Bar"}}

Then we can use {{ data.Id }} and {{ data['Full Name'] }} in the template. 然后,我们可以在模板中使用{{ data.Id }}{{ data['Full Name'] }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM