如何在 Symfony2 或 Twig 中获取资产版本值?

How can I get the assests version value in Symfony2 or Twig?

我需要在我的应用程序的页脚中显示 app/config.yml 文件中设置的资产版本:

framework:
templating:
    engines: ['twig']
    assets_version: v2.6.2

如何在 Twig 或控制器中获取 assets_version 值?

您可以使用 AssetsExtension 的 getAssetsVersion 方法。

因此,在控制器中:

$this->container->get('templating.helper.assets')->getVersion();

或者在 Twig 文件中:

{{ assets_version() }}

勾选https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/TwigBundle/Extension/AssetsExtension.php

如果您使用最新的 Symfony 版本并且您在 config.yml 上的配置如下所示

framework:
    assets:
        version: "your_assets_version"

您可以使用:

{{ asset_version('') }}

查看更多详情https://symfony.com/doc/current/reference/twig_reference.html#asset-version