Symfony2:如何用全局Twig变量替换某个字符串?

use*_*403 2 php symfony twig

我在config.yml中有以下内容

twig:
    globals:
        locale_list_sg: Singapore
        locale_list_cn: China
Run Code Online (Sandbox Code Playgroud)

在我的twig模板中,我希望根据一个参数得到"Singpaore"和"China"的值.因此,我将它们连接起来并显示:

{{'locale_list_' ~ countryId}}
Run Code Online (Sandbox Code Playgroud)

但是,上面将显示locale_list_sg而不是进入config.yml并提取值.

Nic*_*ich 5

使用更简单的方法:

config.yml

twig:
    globals:
        locale_list: 
            sg: Singapore
            cn: China
Run Code Online (Sandbox Code Playgroud)

模板:

{{ locale_list[country_id] }}
Run Code Online (Sandbox Code Playgroud)

  • 它在YAML中定义了一个[associative arrays]数组(http://en.wikipedia.org/wiki/YAML#Lists_of_associative_arrays). (2认同)