[TwigComponent] Parser fails on Twig tags within attributes #2919
Replies: 9 comments
-
What are you trying to do? Something like: <twig:Link {% if class|default %} class="{{ class }}"{% endif %} /> Or something similar ? |
Beta Was this translation helpful? Give feedback.
-
Yes, exactly that. |
Beta Was this translation helpful? Give feedback.
-
You cannot, as you cannot either in include or embed component for instance. Because it would require a loop lexer / parser that does not exist. In your example you "echo" something in Twig, but you cannot have logical "before" the execution of the component. |
Beta Was this translation helpful? Give feedback.
-
Hum yeah, I think we can't solve that easily! To have this TwigComponent syntax we have what we call a Prelexer. It's a little class that goes over your templates and transforms |
Beta Was this translation helpful? Give feedback.
-
That would be really hard to implement this syntax, but for the moment you can spread dynamic properties like that : <twig:Link
{{ ...class is defined ? { class } : {} }}
{{ ...tabindex is defined ? { tabindex } : {} }}
/>
{# or #}
<twig:Link {{ ...{
class: class|default ? class : false,
tabindex: tabindex|default ? tabindex : false,
} }}
/> |
Beta Was this translation helpful? Give feedback.
-
@WebMamba honestly i'm not sure this is easily feasable (and personaly sure it should be done) That would be equivalent to something like |
Beta Was this translation helpful? Give feedback.
-
What about
|
Beta Was this translation helpful? Give feedback.
-
I'm closing here as this limitation is not something we really can work on |
Beta Was this translation helpful? Give feedback.
-
This actually does the right thing! It seems that I missed the part from the docs where it says:
{# templates/components/Input.html.twig #}
<input{{ attributes }}/>
{# render component #}
<twig:Input type="text" value="" :autofocus="false" />
{# renders as: #}
<input type="text" value=""/> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Normally, in a Twig template you can do something like this:
This doesn't seem possible with Twig Components, it breaks at
{% if
or{{
, even if those are commented out:It looks like the parser just bypasses the Twig engine completely and parses the code as raw file instead. I searched through the issues, and it seems you guys are already aware of the problem (for example here: #880 (comment)) and it needs much more rewriting than one might think 🙂
Nevertheless, I thought that may be this specific use-case needs to be mentioned here, just to keep track of everything.
Beta Was this translation helpful? Give feedback.
All reactions