【问题标题】:Asset Function without Symfony in TwigTwig 中没有 Symfony 的资产函数
【发布时间】:2016-07-07 12:36:56
【问题描述】:

我想在我只使用 Twig 的项目中使用资产功能。 我尝试使用它,但 php 将其返回给我:

致命错误:未捕获的异常 'Twig_Error_Syntax' 带有消息“./Default/base.html.twig”中的“未知“资产”函数

【问题讨论】:

    标签: php templates web twig


    【解决方案1】:

    解决方案在sensiolabs.org

    我在conf.php添加了这个:

    //config/config.php
    require_once('../vendor/autoload.php');
    
    //twig
    Twig_Autoloader::register();
    $loader = new Twig_Loader_Filesystem('../app/views');
    $twig = new Twig_Environment($loader);
    $twig->addFunction(new \Twig_SimpleFunction('asset', function ($asset) {
        // implement whatever logic you need to determine the asset path
    
        return sprintf('../assets/%s', ltrim($asset, '/'));
    }));
    

    【讨论】:

      【解决方案2】:

      这是因为 Assetic 本身是一个独立的 PHP 库。
      所以要在 twig 中使用 asset,你需要安装包。

      运行以下 composer 命令来安装 assetic bundle

      composer require symfony/assetic-bundle
      

      如果不安装独立包,twig 将无法找到 asset 函数。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 2015-01-24
      • 1970-01-01
      相关资源
      最近更新 更多