用TWIG加载远程文件 [英] Load Remote File with TWIG

查看:143
本文介绍了用TWIG加载远程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Symfony 2.3中使用Twig,我需要能够在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 %}

,如您所见,它试图包含一个远程树枝模板.这是可能的,因为symfony只是错误地指出找不到模板?

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?

在我的代码中,树枝模板的位置是正确的,因为我可以在浏览器中浏览到模板URL.

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远程位置只是我们拥有共享资产的其他Web服务器之一.

P.S 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.

想法:

  • 您创建目录app/Resources/views/temp,因此可以在:temp:file.html.twig
  • 中访问树枝文件
  • 在树枝文件中,您将使用remote_file()函数包装第一个include的参数
  • 您的文件将通过您的函数在temp目录中以随机名称
  • 下载.
  • 您的函数将返回一条细枝路径以本地访问文件(:temp:file.html.twig)
  • 不要忘记自动执行一些操作以清除太旧的临时文件! (一个cron吗?)
  • 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?)

创建一个临时目录,以便您的symfony目录树如下所示:

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

在包中,创建一个Twig\Extension目录.在此处,使用以下代码创建一个RemoteFileExtension.php文件.注意:别忘了用您的命名空间替换我的命名空间.

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';
    }

}

在您的services.yml配置文件中,添加以下内容:

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

parameters以下:

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

services以下:

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

测试!

我创建了一个现有的http://localhost:8888/test.html.twig.它仅包含:

Hello, {{ name }}! 

在我的应用程序中,我放置了以下行:

Inside my application, I put the following line :

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

运行代码时,我得到:

您应该考虑将树枝文件包含在您的应用程序中.细枝文件不是资产,因为Symfony2需要使用一些逻辑,一些优化等来解释它.您实际上在做什么,相当于在执行之前远程包含一个PHP文件,我认为这是一种奇怪的体系结构.

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.

这篇关于用TWIG加载远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆