Load Remote File with TWIG

怎甘沉沦 提交于 2020-01-01 06:25:15

问题


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. =)

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


回答1:


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.



来源:https://stackoverflow.com/questions/19030721/load-remote-file-with-twig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!