phpsymfonytemplatestwigsymfony-http-kernel

Get kernel root dir in Twig template with Symfony 2


I need to get kernel.root_dir in my twig template, I found solutions here, but it does not work. I get error about non existing method Kernel in this class. Realy in GlobalVariables class method getKernel() not exists. So how I can get root_dir in twig template.

(P.S. No I cant get it form controller, and no I cant get it as container parameters)


Solution

  • I suggest you to add a global variable in your config.yml:

    # app/config/config.yml
    twig:
        # ...
        globals:
            kernelRootDir: "%kernel.root_dir%"
    

    Then, use {{ kernelRootDir }} in your views.