David Morales
David Morales

Reputation: 18064

Including a non-twig file from twig

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/directory/file.ext' %}

Isn't Twig capable of this? (I don't want to use Assetic)

Upvotes: 9

Views: 20140

Answers (4)

Igor Paladino
Igor Paladino

Reputation: 159

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

<?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 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

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

Upvotes: 1

Disparity
Disparity

Reputation: 211

New in version 1.15: The source function was added in Twig 1.15. The source function returns the content of a template without rendering it

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

Upvotes: 17

kgilden
kgilden

Reputation: 10346

I made a bundle just for this some time ago. It's pretty much a wrapper for file_get_contents.

The setup can be found here.

Upvotes: 3

Matt
Matt

Reputation: 10823

Twig will complain when loading your file if it is not a valid twig template. The reason is that Twig will include the rendered file and not the content of it (found here).

You could try with a use statement but I don't think this will work either.

Moreover, the syntax you use seems incorrect. When I include (or use) another twig template, I use this syntax:

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

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

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

If that doesn't work, you could possibly move the file into the views folder. If this is not possible, you can possibliy put in app\Resources\views\ and use the syntax:

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

If the use statement doesn't work, which I'm afraid of, you could possibly wrap your file into a twig template directly. I'm templating some simple JSON structures with twig. So, there may be a way for you to include the content of file.ext into a twig template and then render it.

If all this fail, you will have to create a Twig extension that add a new tag (something like content) that would read a file and output its content in your twig template.

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

Hope this will help you to include your file.

Regards,
Matt

Upvotes: 1

Related Questions