如何将计算结果连接到Twig中的某些现有文本?

时间:2017-08-08 14:34:23

标签: php symfony twig

我有几个可用的变量可以让我显示年份。例如,

{{ vars['cotisation_max_year'] }} => 2016

我有另一个变量如下所示:

{{ vars.cotisations['cotisation_2014'] }}

显示查询结果。

如果我{{ vars['cotisation_max_year'] - 1 }},我会2015年,如果我这样做

{% set annee = vars['cotisation_max_year'] %} // Let's simplify
{{ vars.cotisations['cotisation_' ~ annee ] }}

我得到2016年的结果。但是,如果我执行以下操作:

{{ vars.cotisations['cotisation_' ~ annee - 1 ] }}

什么都没有出现。

有没有办法显示结果呢?我无法在Twig文档或SO

上找到任何可以解决我问题的方法

1 个答案:

答案 0 :(得分:3)

尝试这样做而不是使代码复杂化:

0.016666666666666666          
515                           
16                            
16                            
17                            
17                            
16                            
16                            
17                            
16                            
16                            
16                            
17                            
17                            
16                            
17                            

或者这个(从未尝试过):

{% set annee = vars['cotisation_max_year'] -1 %} // Let's simplify
{{ vars.cotisations['cotisation_' ~ annee ] }}