包含来自 twig 的非 twig 文件

发布于 2024-12-21 21:56:39 字数 167 浏览 1 评论 0原文

我需要将文件的内容(在我的资源文件夹内)包含在 Twig 模板中。

我尝试过,但没有成功:

{% include 'public/directory/file.ext' %}

Twig 不能做到这一点吗? (我不想使用Assetic)

I need to include the contents of a file (inside my resources folder) inside a Twig template.

I have tried this with no luck:

{% include 'public/directory/file.ext' %}

Isn't Twig capable of this? (I don't want to use Assetic)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

给我一枪 2024-12-28 21:56:39

1.15版本新增:Twig 1.15中添加了source功能。
源函数返回模板的内容而不渲染它

http://twig.sensiolabs.org /doc/functions/source.html

New in version 1.15: The source function was added in Twig 1.15.
The source function returns the content of a template without rendering it

http://twig.sensiolabs.org/doc/functions/source.html

另类 2024-12-28 21:56:39

我前段时间为此制作了一个 bundle 。它几乎是 file_get_contents 的包装。

可以在此处找到该设置。

I made a bundle just for this some time ago. It's pretty much a wrapper for file_get_contents.

The setup can be found here.

ゝ偶尔ゞ 2024-12-28 21:56:39

如果加载文件时 Twig 不是有效的 Twig 模板,Twig 会抱怨。原因是 Twig 将包含渲染的文件而不是它的内容(发现 此处)。

您可以尝试使用 use 语句,但我认为这也不起作用。

此外,您使用的语法似乎不正确。当我包含(或使用)另一个树枝模板时,我使用以下语法:

{% use "AcmeWebsiteBundle::include.html.twig" %}

文件 include.html.twig 位于 src\Acme\WebsiteBundle\Resources\views\include.html 中。树枝。因此,如果您的文件位于 src\Acme\WebsiteBundle\Resources\public\directory\include.ext 中,您可以尝试

{% use "AcmeWebsiteBundle::..\public\directory\include.ext" %}

如果这不起作用,您可以将文件移动到视图文件夹中。如果这是不可能的,您可以放入 app\Resources\views\ 并使用语法:

{% use "::include.ext" %}

如果 use 语句不起作用,我担心,您可以将您的直接文件到树枝模板中。我正在使用 twig 构建一些简单的 JSON 结构模板。因此,您可能有一种方法可以将 file.ext 的内容包含到 twig 模板中,然后渲染它。

如果这一切都失败了,您将必须创建一个 Twig 扩展,添加一个新标签(例如 content),该标签将读取文件并将其内容输出到您的 Twig 模板中。

{% content 'public/directory/file.ext' %} {# would put content of the file #}

希望这会帮助您包含您的文件。

问候,
马特

Twig will complain when loading your file if it is not a valid twig template. The reason is that Twig will include the rendered file and not the content of it (found here).

You could try with a use statement but I don't think this will work either.

Moreover, the syntax you use seems incorrect. When I include (or use) another twig template, I use this syntax:

{% use "AcmeWebsiteBundle::include.html.twig" %}

And the file include.html.twig resideds in src\Acme\WebsiteBundle\Resources\views\include.html.twig. So, if your file is in src\Acme\WebsiteBundle\Resources\public\directory\include.ext, you could try

{% use "AcmeWebsiteBundle::..\public\directory\include.ext" %}

If that doesn't work, you could possibly move the file into the views folder. If this is not possible, you can possibliy put in app\Resources\views\ and use the syntax:

{% use "::include.ext" %}

If the use statement doesn't work, which I'm afraid of, you could possibly wrap your file into a twig template directly. I'm templating some simple JSON structures with twig. So, there may be a way for you to include the content of file.ext into a twig template and then render it.

If all this fail, you will have to create a Twig extension that add a new tag (something like content) that would read a file and output its content in your twig template.

{% content 'public/directory/file.ext' %} {# would put content of the file #}

Hope this will help you to include your file.

Regards,
Matt

玩心态 2024-12-28 21:56:39

您可以使用 twig 扩展函数来解决这个问题。 ( http://symfony.com/doc/current/cookbook/templated/twig_extension。 html

在 Bundle 中为 Twig 扩展创建一个 php 文件

<?php

namespace AppBundle\Twig;

use Twig_Extension;
use Twig_SimpleFunction;

class AppExtension extends Twig_Extension
{
  public function getFunctions()
  {
    return array(
      new Twig_SimpleFunction('fileGetContents', array($this, 'fileGetContents') ),
    );
  }

  public function fileGetContents($file)
  {
    return file_get_contents($file);
  }

  public function getName()
  {
    return 'app_extension';
  }
}
?>

将此代码添加到 app/config/services.yml

app.twig_extension:
    class: AppBundle\Twig\AppExtension
    public: false
    tags:
        - { name: twig.extension }

现在,您可以使用自定义函数作为 twig 模板中的任何默认函数

<p>{{ fileGetContents( asset("uploads/my_text_file.txt") ) }}</p>

You can solve this problem with twig extension functions. ( http://symfony.com/doc/current/cookbook/templating/twig_extension.html )

Create a php file in your Bundle for the Twig extension

<?php

namespace AppBundle\Twig;

use Twig_Extension;
use Twig_SimpleFunction;

class AppExtension extends Twig_Extension
{
  public function getFunctions()
  {
    return array(
      new Twig_SimpleFunction('fileGetContents', array($this, 'fileGetContents') ),
    );
  }

  public function fileGetContents($file)
  {
    return file_get_contents($file);
  }

  public function getName()
  {
    return 'app_extension';
  }
}
?>

Add this code to the app/config/services.yml

app.twig_extension:
    class: AppBundle\Twig\AppExtension
    public: false
    tags:
        - { name: twig.extension }

Now you can use your custom function as any default function in your twig template

<p>{{ fileGetContents( asset("uploads/my_text_file.txt") ) }}</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文