twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.18k stars 1.25k forks source link

Question: Transform HTML like Tag to TWIG tag #3870

Closed AndreRudolph closed 11 months ago

AndreRudolph commented 1 year ago

I am looking for a hook in twig engine with that I can transform something like

<foo bar="baz" />

into

{{ func("foo", { bar: "baz" } ) }}

I am not sure how I can achieve this and where in the compilation process this must happen. Is this a NodeVisitor or rather responsibility of a TokenParser? Or something completely else? Should this maybe happen at FileLoader / Cache level and with regex replacing? Unfortunately the twig documentation does not cover such a topic in depth.

Looking forward for any advices and tips, thank you.

PS: I know I could just simply put the second snippet in the template but I want to provide the first snippet as some kind of nice-to-have option that will be converted to the second snippet.

kbond commented 1 year ago

Hey @AndreRudolph, you can take a look at what we did in ux-twig-components: https://symfony.com/bundles/ux-twig-component/current/index.html#component-html-syntax

fabpot commented 11 months ago

Closing in favor of #3867