Including a non-twig file from twig

前端 未结 4 1795
粉色の甜心
粉色の甜心 2021-02-14 19:51

I need to include the contents of a file (inside my resources folder) inside a Twig template.

I have tried this with no luck:

{% include \'public/directo         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-14 20:37

    You can solve this problem with twig extension functions. ( http://symfony.com/doc/current/cookbook/templating/twig_extension.html )

    Create a php file in your Bundle for the Twig extension

    
    

    Add this code to the app/config/services.yml

    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        public: false
        tags:
            - { name: twig.extension }
    

    Now you can use your custom function as any default function in your twig template

    {{ fileGetContents( asset("uploads/my_text_file.txt") ) }}

提交回复
热议问题