简体   繁体   English

插入Twig变量/在另一个Twig属性定义的内部值中进行插值

[英]Interpolate Twig variable/attribute inside value of another Twig attribute definition

Take the following example structure: 采取以下示例结构:

{% set paths = {
                   ...
                   'js': {
                           ...
                           'jquery': {
                                      'version': '1.7.2',
                                      'cdn': 'https://ajax.googleapis.com/ajax/libs/jquery/{{paths.js.jquery.version}}/jquery.min.js',
                                      'fallback': ....
                                      }
                          }
               }
%}

To access, I would normally use something like: 要访问,我通常会使用类似:

 <script src="{{paths.js.jquery.cdn}}"></script>

The problem: 问题:

The interpolated variable isn't recognized, you will get something like ...libs/jquery/%7B%7B%paths.js/jquery.version7D%7D/jquery.min.js... . 无法识别插值变量,您将得到类似...libs/jquery/%7B%7B%paths.js/jquery.version7D%7D/jquery.min.js...

I've tried: 我试过了:

  • 'a': 'text{{b}}text' , 'a': 'text{{b}}text'
  • 'a': {{ ('text' ~ b ~ 'text') }} , 'a': {{ ('text' ~ b ~ 'text') }}
  • 'a': "{{ ('text' ~ b ~ 'text') }}" , 'a': "{{ ('text' ~ b ~ 'text') }}"
  • 'a': "{{ (['text', b, 'text'] | join }}" , 'a': "{{ (['text', b, 'text'] | join }}"
  • 'a': "{{ (['text', {{b}}, 'text'] | join }}"
  • And more I've forgotten 还有更多我忘记的事情

I'm aware of attribute() 我知道attribute()

There's not a lot of documentation on it, but from what I see, it would have to be something like this: 没有太多的文档,但是从我的角度来看,它一定是这样的:

attribute(paths, attribute(js, attribute(jquery, cdn)))

It would be fine for one level, but not an arbitrary level of depth. 只适合一个级别,但不适合任意深度。 Please correct me if I'm misunderstanding attribute() . 如果我误解了attribute()请纠正我。

'a': ('text' ~ b ~ 'text' ) is actually correct, but there cannot be curly braces around the expression (because we're already inside an expression, a variable definition). 'a': ('text' ~ b ~ 'text' )实际上是正确的,但是表达式周围不能有花括号(因为我们已经在表达式内部,变量定义了)。

The correct way: 正确的方法:

{% set paths = {
                   ...
                   'js': {
                           ...
                           'jquery': {
                                      'version': '1.7.2',
                                      'cdn': ('https://ajax.googleapis.com/ajax/libs/jquery/' ~ paths.js.jquery.version ~ '/jquery.min.js'),
                                      'fallback': ....
                                      }
                          }
               }
%}

But there is simple design mistake here; 但是这里存在简单的设计错误; when the renderer/parser comes to the cdn attribute definition, paths isn't set yet. 当渲染器/解析器进入cdn属性定义时,尚未设置paths One possible way to get around this is to declare another variable first; 解决这个问题的一种可能方法是先声明另一个变量。

{% set params=  {
                   'jqueryversion': '1.7.2'
                }
%}

{% set paths = {
                   ...
                   'js': {
                           ...
                           'jquery': {
                                      'cdn': ('https://ajax.googleapis.com/ajax/libs/jquery/' ~ params.jqueryversion ~ '/jquery.min.js'),
                                      'fallback': ....
                                      }
                          }
               }
%}

Obviously if paths.js.jquery.cdn is only going to be used once then just hardcode the parent variable's value where you need it and interpolate the child variable: 显然,如果paths.js.jquery.cdn只会使用一次,然后就硬编码在你需要它和插值孩子变量变量的值:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/{{params.jqueryversion}}/jquery.min.js"></script>

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

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