Reputation: 3
I would like to include a raw file (user-actions.log
) to a Template.
The file is saved in /MyBundle/Ressources/views/Admin/user-actions.log
(could change directory if necessary)
Trying to include file with {{ include('MyBundle:Admin:user-actions.log) }}
or {% include 'MyBundle:Admin:user-actions.log' }}
doesnt work because it isn't a .twig
.
How can I solve this?
Upvotes: 0
Views: 465
Reputation: 494
Write up an twig extension
<?php
namespace Acme\AcmeBundle\Twig;
use Symfony\Component\HttpKernel\KernelInterface;
class Extension extends \Twig_Extension
{
private $kernel;
private $service_container;
public function __construct(KernelInterface $kernel, $service_container)
{
$this->kernel = $kernel;
$this->service_container = $service_container;
}
public function getFunctions()
{
return array(
'includeFile' => new \Twig_Function_Method($this, 'includeFile')
);
}
public function includeFile($file)
{
return file_get_contents($file);
}
}
In services.yml
parameters:
acme.twig.extension.class: Acme\AcmeBundle\Twig\Extension
services:
acme.twig.extension:
class: %acme.twig.extension.class%
arguments:
kernel: "@kernel"
service_container: "@service_container"
tags:
- { name: twig.extension }
In your twig templates you can use
{{ includeFile(your_path_to_the_file.log) }}
More docs http://symfony.com/doc/current/cookbook/templating/twig_extension.html
Upvotes: 3