How can I modify/extend a global TWIG variable?

时间:2017-03-22 18:40:03

标签: php symfony twig yaml

I know you can set global variables in TWIG this way :

# config.yml
# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    cache : false
    globals :
        server : 127.0.0.1

Now what I want is to create a custom variable server that uses the preexistent global variable app.request.host. I can reference that variable in a TWIG template, but how can I do that in a YML file ?

In one sentence : I want to create a global TWIG variable from another one.

1 个答案:

答案 0 :(得分:1)

为什么不参加参数。 只需将它放在你的parameters.yml

server: 127.0.0.1

之后你可以在yml文件中引用它,如下所示:

"%server%"

将它添加到全局变量中:

twig:
    globals:
        server: '%server%'

如果你需要在控制器中,那么使用这种方法:

$this->getParameter("server")