简体   繁体   中英

Load Remote File with TWIG

Using Twig in Symfony 2.3 I need to be able to select remote assets inside a twig template.

So I have a twig template with a body block like this:

{% block body %}
    {% include 'http://asset.remotelocation.co.uk/template.html.twig' %}
{% endblock %}

and as you can see its trying to include a remote twig template. Is this possible as symfony just errors saying it cant find the template?

The twig template location is correct in my code as I can browse to the template url in my browser.

Any help is much appeciated. =)

PS the remote location is just one of our other webserver where we hold shared assets.

You can create a function that will download this file for you, and make it available for twig.

The idea :

  • You create a directory app/Resources/views/temp , so a twig files can be accessed at :temp:file.html.twig
  • In your twig file, you'll use a remote_file() function to wrap the first include 's argument
  • Your files will be downloaded by your function inside temp directory with a random name
  • your function will return a twig path to access the file locally (the :temp:file.html.twig )
  • don't forget to automate something to clear too old temp files! (a cron?)

The directory

Create a temp directory so that your symfony directory tree looks like this:

在此处输入图片说明

The extension

Inside your bundle, create a Twig\\Extension directory. There, create a RemoteFileExtension.php file with the following code. Note: don't forget to replace my namespaces by yours.

<?php

namespace Fuz\TestBundle\Twig\Extension;

use Symfony\Component\HttpKernel\KernelInterface;

class RemoteFileExtension extends \Twig_Extension
{

    private $kernel;

    public function __construct(KernelInterface $kernel)
    {
        $this->kernel = $kernel;
    }

    public function getFunctions()
    {
        return array(
                'remote_file' => new \Twig_Function_Method($this, 'remote_file'),
        );
    }

    public function remote_file($url)
    {
        $contents = file_get_contents($url);
        $file = $this->kernel->getRootDir() . "/Resources/views/temp/" . sha1($contents) . '.html.twig';
        if (!is_file($file))
        {
            file_put_contents($file, $contents);
        }
        return ':temp:' . basename($file);
    }

    public function getName()
    {
        return 'remote_file';
    }

}

Inside your services.yml configuration file, add the following:

below parameters :

fuz_tools.twig.remote_file_extension.class: Fuz\TestBundle\Twig\Extension\RemoteFileExtension

below services :

fuz_tools.twig.remote_file_extension:
    class: '%fuz_tools.twig.remote_file_extension.class%'
    arguments: ['@kernel']
    tags:
      - { name: twig.extension }

Test it!

I created an existing http://localhost:8888/test.html.twig . It only contains:

Hello, {{ name }}! 

Inside my application, I put the following line :

{% include remote_file('http://localhost:8888/test.html.twig') with {'name': 'Alain'} %}

When I run my code, I get :

在此处输入图片说明

Some more notes

You should consider that a twig file takes part of your application. A twig file is not an asset, as it needs to be intepreted by Symfony2, with some logic, with some optimizations and so on. What you're doing actually is comparable to a remote include of a PHP file before executing it, strange architecture I think.

Anyway, your question was interesting, good luck for your implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM