沃梦达 / 编程问答 / php问题 / 正文

使用 TWIG 加载远程文件

Load Remote File with TWIG(使用 TWIG 加载远程文件)

本文介绍了使用 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?

twig 模板位置在我的代码中是正确的,因为我可以在浏览器中浏览到模板 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 远程位置只是我们持有共享资产的其他网络服务器之一.

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

推荐答案

您可以创建一个函数来为您下载此文件,并使其可用于 twig.

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

想法:

  • 你创建一个目录app/Resources/views/temp,这样一个twig文件就可以在:temp:file.html.twig
  • 在您的 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:

在你的包中,创建一个 TwigExtension 目录.在那里,使用以下代码创建一个 RemoteFileExtension.php 文件.注意:不要忘记用你的替换我的命名空间.

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

<?php

namespace FuzTestBundleTwigExtension;

use SymfonyComponentHttpKernelKernelInterface;

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:

参数下方:

fuz_tools.twig.remote_file_extension.class: FuzTestBundleTwigExtensionRemoteFileExtension

服务下方:

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

当我运行我的代码时,我得到:

When I run my code, I get :

您应该考虑到 twig 文件是您的应用程序的一部分.twig 文件不是资产,因为它需要由 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 加载远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 TWIG 加载远程文件

基础教程推荐