zenstruck / twig-service-bundle

Make functions, static methods, Symfony service methods available in your twig templates.
MIT License
10 stars 2 forks source link

make prefix configurable, and allow no prefix #18

Open tacman opened 2 months ago

tacman commented 2 months ago

Re: #17

I'd prefer to make the twig calls without the fn prefix. Or perhaps to prefix my application twig functions with app, for example.

It's awkward to need the fn, but since that's how it currently works, maybe you could make it configurable and default to fn , but allow none.

    # injected into twig via
    #[AsTwigFunction('source_icon')] # fn_source_icon
    static public function getSourceIcon(string $source): ?string
zenstruck_twig_service:
  functions:
    - iterator_to_array # available as "fn_iterator_to_array()" in twig
    - json_decode #fn_json_decode
    - file_exists

Could be

zenstruck_twig_service:
 prefix: false # or "" 
 functions:
    - iterator_to_array # available as "iterator_to_array()" in twig
    - json_decode # json_decode
    - file_exists
tacman commented 1 month ago

Thoughts on this, @kbond? Or am I doing something wrong? I think the documentation is wrong, but I'd rather the code change to match the documentation, as I'd like to get rid of the fn_ prefix.

kbond commented 1 month ago

I need the prefix because of the twig feature I'm using to achieve this: https://twig.symfony.com/doc/3.x/advanced.html#dynamic-filters

While possible to add these functions directly to twig, this would reduce performance - especially when adding many functions.