0

I'm nearly new in Symfony2 and I have a little question:

I'm developing an email template, which has txt and html parts (no problem with it)

The only 'problem' I have is with the absolute paths of assets in TWIG.

Inside my email.html.twig file I have something like this:

<img src="{{ asset('images/my-image.png') }}" alt="My image" /> but it writes the route with relative path.

I discovered a little solution to add absolute paths, something like this:

{% set abs = app.request.scheme ~  '://' ~ app.request.host %}
<img src="{{ abs ~ asset('images/my-image.png') }}" alt="My image" />

It works! But I want to improve this solution and also learn to create custom filters (I read the documentation, but I got a bit lost)

I want to create something like this:

<img src="{{ asset('images/my-image.png' | absolute) }}" alt="My image" />

But I don't know how to properly override the assetics extension. Can you help me?

Thanks a lot!!

4

1 回答 1

1

嗯,复制粘贴解决方案有点困难,但我可以制作一本简短的食谱,这样你就可以一步一步自己做:

1) 你必须实现 Assetic/Filter/FilterInterface

2)如果你看一下FilterInterface类,你会发现你必须实现两个方法:filterLoad和filterDump。

所以,你会做这样的事情:

<?php

namespace You\YourBundle\Assetic\Filter;

use Assetic\Asset\AssetInterface;
use Assetic\Filter\FilterInterface;

class YourAsseticFilter implements FilterInterface
{ 
    public function filterLoad(AssetInterface $asset)
    {

          // do something

    }


    public function filterDump(AssetInterface $asset)
    {
        $content = $asset->getContent();

        // do something

        $asset->setContent($content);
    }


}

在您完成此操作之后,您必须执行与在 YourBundle 中的 services.yml 中注册 twig 扩展非常相似的操作。当然,这取决于您是否使用 YML、XML... 配置。我使用 yml,所以我会输入 yml :)

parameters:
    your_bundle.class: You\YourBundle\Assetic\Filter\YourAsseticFilter

services:
    your_bundle.assetic.your_assetic_filter:
        class: %your_bundle.class%
        tags:
            - { name: assetic.filter }
            - { alias: yourChosenNameForYourNewAsseticFilter }

然后你称之为 | 当然,yourChosenNameForYourNewAsseticFilter。

于 2013-07-08T07:11:38.267 回答