symfony - Symfony2 Twig custom filter in asset -
i'm new in symfony2 , have little question:
i'm developing email template, has txt , html parts (no problem it)
the 'problem' have absolute paths of assets in twig.
inside email.html.twig file have this:
<img src="{{ asset('images/my-image.png') }}" alt="my image" />
writes route relative path.
i discovered little solution add absolute paths, this:
{% set abs = app.request.scheme ~ '://' ~ app.request.host %} <img src="{{ abs ~ asset('images/my-image.png') }}" alt="my image" />
it works! want improve solution , learn create custom filters (i read documentation, got bit lost)
i want create this:
<img src="{{ asset('images/my-image.png' | absolute) }}" alt="my image" />
but don't know how override assetics extension. can me?
thanks lot!!
well, little hard copy paste solution can make short cookbook can go step step , yourself:
1) have implement assetic/filter/filterinterface
2) if @ filterinterface class, see have to implement 2 methods: filterload , filterdump.
so, this:
<?php namespace you\yourbundle\assetic\filter; use assetic\asset\assetinterface; use assetic\filter\filterinterface; class yourasseticfilter implements filterinterface { public function filterload(assetinterface $asset) { // } public function filterdump(assetinterface $asset) { $content = $asset->getcontent(); // $asset->setcontent($content); } }
and after this, have similar registering twig extensions in services.yml in yourbundle. sure, depens if use yml, xml... configuration. use yml type in 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 }
and call | yourchosennameforyournewasseticfilter, of course.
Comments
Post a Comment