Including a file with a branch from a branch

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

I tried this with no luck:

{% include 'public/directory/file.ext' %} 

Isn't that a twig? (I do not want to use Assetic)

+6
source share
4 answers

I did bundle just for this a while ago. This is a pretty wrapper for file_get_contents .

Settings can be found here .

+3
source

New in version 1.15: Source function added in Twig 1.15. The source function returns the contents of the template without rendering it.

http://twig.sensiolabs.org/doc/functions/source.html

+9
source

Twig will complain when downloading a file if it is not a valid branch template. The reason is that Twig will include the render file rather than its contents (found here ).

You can try with the use statement, but I don't think this will work.

Also, the syntax you are using seems wrong. When I include (or use) another branch template, I use this syntax:

 {% use "AcmeWebsiteBundle::include.html.twig" %} 

And the include.html.twig file is in src\Acme\WebsiteBundle\Resources\views\include.html.twig . So, if your file is located in src\Acme\WebsiteBundle\Resources\public\directory\include.ext , you can try

 {% use "AcmeWebsiteBundle::..\public\directory\include.ext" %} 

If this does not work, you can transfer the file to the views folder. If this is not possible, you can install app\Resources\views\ and use the syntax:

 {% use "::include.ext" %} 

If the usage instruction does not work, which I'm afraid you can transfer your file to the branch template. I draw some simple JSON structures with a twig. That way, you can include the contents of file.ext in the branch template and then render it.

If all this fails, you will need to create a Twig extension that will add a new tag (something like content ) that will read the file and output its contents to your branch template.

 {% content 'public/directory/file.ext' %} {# would put content of the file #} 

Hope this helps you include your file.

Respectfully,
Matt

+1
source

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

Create php file in your bundle for Twig extension

 <?php namespace AppBundle\Twig; use Twig_Extension; use Twig_SimpleFunction; class AppExtension extends Twig_Extension { public function getFunctions() { return array( new Twig_SimpleFunction('fileGetContents', array($this, 'fileGetContents') ), ); } public function fileGetContents($file) { return file_get_contents($file); } public function getName() { return 'app_extension'; } } ?> 

Add this code to app / config / services.yml

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

Now you can use your custom function like any default function in a branch template

 <p>{{ fileGetContents( asset("uploads/my_text_file.txt") ) }}</p> 
0
source

Source: https://habr.com/ru/post/904163/


All Articles