5

我有一个类扩展\Twig_Extension如下:

class MYTwigExtension extends \Twig_Extension
{

    protected $doctrine;

    protected $router;


    public function __construct(RegistryInterface $doctrine , $router)
    {
        $this->doctrine = $doctrine;
        $this->router = $router;

    }

    public function auth_links($user , $request)
    {
       // Some other codes here ...

       // HOW TO GENERATE $iconlink which is like '/path/to/an/image'

       $html .= "<img src=\"$iconlink\" alt=\"\" />  ";  

       echo $html;
    }

}

我的问题是如何在 Twig 扩展中生成资产链接?我想替换我班上的资产助手。基本上我不知道我必须在这里注入或使用什么!提前致谢。

 <img src="{{ asset('img/icons/modules/timesheet.png') }}" alt="" /> 
4

5 回答 5

17

您可以直接使用templating.helper.assets服务。

use Symfony\Component\DependencyInjection\ContainerInterface;

public function __construct(ContainerInterface $container)
{
    $this->container = $container;
}

并像这样使用它:

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

在这种情况下,直接注入templating.helper.assets不起作用,因为 twig 扩展不能在请求范围内。请参阅此处的文档:https ://symfony.com/doc/2.3/cookbook/service_container/scopes.html#using-a-service-from-a-narrower-scope

于 2012-06-14T18:32:13.407 回答
7

我不想处理依赖注入容器。这就是我所做的:

use Twig_Environment as Environment;

class MyTwigExtension extends \Twig_Extension
{
    protected $twig;
    protected $assetFunction;

    public function initRuntime(Environment $twig)
    {
        $this->twig = $twig;
    }

    protected function asset($asset)
    {
        if (empty($this->assetFunction)) {
             $this->assetFunction = $this->twig->getFunction('asset')->getCallable();
        }
        return call_user_func($this->assetFunction, $asset);
    }

我查看了Twig_Extension类代码,并在initRuntime那里找到了这个方法,可以在我们的自定义扩展类中被覆盖。它接收Twig_Environment作为参数!这个对象有一个getFunction方法,它返回一个Twig_Function实例。我们只需要传递函数名(asset在我们的例子中是 )。

Twig_Function对象有一个getCallable方法,所以我们最终可以有一个可调用的函数asset

我进一步为我自己的扩展类创建了一个asset方法。在它的其他任何地方,我都可以简单地调用并获得与模板中$this->asset()相同的结果。{{ asset() }}

编辑:清除缓存时,getFunction调用initRuntime会引发范围异常。所以我把它移到了自定义asset方法。它工作正常。

于 2013-08-14T17:14:14.380 回答
3

这是 Symfony 2.8 的一种简单而干净的方法:

服务.yml:

app.twig_extension:
    class: Path\To\AcmeExtension
    arguments:
        assets: "@templating.helper.assets"

在 TWIG 扩展中:

use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;

class AcmeExtension
{
    protected $assets;

    public function __construct(AssetsHelper $assets)
    {
        $this->assets = $assets;
    }
}

然后您可以在扩展的任何功能中使用它,如下所示:

$this->assets->getUrl('myurl');
于 2016-04-10T16:17:36.987 回答
1

在对我有用的Symfony 5.3中:(只需做资产扩展所做的事情并注入包)


use Symfony\Component\Asset\Packages;
use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;

class AppExtension extends AbstractExtension implements ExtensionInterface
{
    public function __construct(Packages $packages)
    {
        $this->packages = $packages;
    }

    // ... your other methods

    private function asset($path, $packageName = null)
    {
        return $this->packages->getUrl($path, $packageName);
    }
}
于 2021-11-10T15:00:29.407 回答
0

在适用于我的Symfony 2.8中:

# services.yml
services:
    app.twig_extension:
        class: AppBundle\Twig\AppTwigExtension
        public: false
        arguments:
            - @templating.helper.assets
        tags:
            - { name: twig.extension }

AppTwigExtension 类:

namespace AppBundle\Twig;

use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;

/**
 * Class AppTwigExtension
 * @package AppBundle\Twig
 */
class AppTwigExtension extends \Twig_Extension
{
    const IMG_PATH = 'bundles/app/images/';

    private $assetsHelper;

    public function __construct(AssetsHelper $assetsHelper)
    {
        $this->assetsHelper = $assetsHelper;
    }

    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('img', array($this, 'imagePathFilter'))
        );
    }

    /**
     * Get image path relatively to host
     * Usage in Twig template: {{ 'my_image.png'|img }} - equal to
     * {{ asset('bundles/app/images/my_image.png') }} in Twig template:
     *
     * @param string $imageName (e.g. my_image.png)
     * @return string
     */
    public function imagePathFilter($imageName)
    {
        return $this->assetsHelper->getUrl(self::IMG_PATH . $imageName);
    }

    public function getName()
    {
        return 'app_twig_extension';
    }
}
于 2016-02-18T21:27:01.410 回答