Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Symfony2 filter not triggering with custom twig tag

TL:DR; Adding type="application/dart" makes Assetic ignore the filter flag filter=MyCustomFilter.

Removing the latter attribute type triggers filter=MyCustomFilter just fine. But I need the filter and the attribute. How do I make Assetic trigger my custom filter while having the type=application/dart attribute?

I believe part of the problem is that it only accepts type=application/javascript or an empty html type attribute for the filter to trigger. I'm not sure where to go from here.


Full details:

I want to make a tag similar to javascripts but for Dart files.

{% darts
    '@AcmeBundle/Resources/dart/AcmeMain/web/main.dart'
%}$ 
    <script type="application/dart" src="{{ asset_url }}"></script>
{% enddarts %}

I want to be able to use the @ notation instead of /bundles/etc/

After a bit of searching, I tried the following:

I extended Symfony\Bundle\AsseticBundle\Twig\AsseticExtension

I inherited the following function:

public function getTokenParsers()
{
    return array(
        $this->createTokenParser('javascripts', 'js/*.js'),
        $this->createTokenParser('stylesheets', 'css/*.css'),
        $this->createTokenParser('image', 'images/*', true),
    );
}

In my own child class I added to the parent array $this->createTokenParser('darts', 'dart/*.dart').

Initially this works when I have.

{% darts '@MyBundle\...\main.dart' filter="MyCustomFilter" %}
<script src="{{ asset_url }}"></script>

However, the bootloader dart.js requires as type="application/dart attribute in order for the bootstrap to work.

As soon as I add the required attribute, editing the source file does not recompile. It's pretty much ignored. The following code is ignored and does not trigger MyCustomFilter filter:

{% darts '@MyBundle\...\main.dart' filter="MyCustomFilter" %}
<script type="application/dart" src="{{ asset_url }}"></script>

I can't seem to find where to go next from here, seeing how it all works is pretty overwhelming in itself. I only need it to recognize type="application/dart as valid so that my MyCustomFilter gets triggered.

like image 399
Tek Avatar asked Jan 15 '15 18:01

Tek


1 Answers

I suggest you to have a look to vendor/symfony/assetic-bundle/ files as it contains the {% javascripts %} implementation.

From that bundle's Resources/config/templating_twig.yml, they inject a service named @templating.name_parser, it looks like that's just what you are looking for.

You can have a try in a controller to do:

var_dump(
  $this
    ->get('templating.name_parser')
    ->parse('@AcmeBundle/Resources/dart/AcmeMain/web/main.dart')
    ->getPath()
);

About the tag itself, you can heavily copy/paste from the assetic bundle. And get some explanations about custom tags over one of my previous answers, here.

like image 69
Alain Tiemblo Avatar answered Sep 28 '22 02:09

Alain Tiemblo