Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Html" Lexer #3867

Open
kbond opened this issue Aug 1, 2023 · 6 comments
Open

"Html" Lexer #3867

kbond opened this issue Aug 1, 2023 · 6 comments

Comments

@kbond
Copy link
Contributor

kbond commented Aug 1, 2023

With the popularity of Twig Component HTML Syntax I wanted to discuss the possibility of adding an official html lexer in Twig core. What we're doing in twig components is sort of a hack: converting <twig:... to {% .... An official lexer would remove this conversion process.

We've already added the block tag (<twig:block name="title">) in twig components. We can envision users asking for more official Twig tags as html tags.

I know this would not be an easy or quick task and there would be a lot to consider for each tag:

<twig:if ...?>
    ...
<twig:else>
    ...
<twig:endif>

Alternatively, if it's decided to not include in core, what about opening the lexer up for extension?

@weaverryan
Copy link
Contributor

To clarify, there are 2 possible levels to this proposal

  1. We add this HTML lexing to core and allow for things like <twig:if> as Kevin said.

  2. OR... we add something (in core? Or via an extension in Lexer) to support the <twig:Foo> syntax properly in a way where TwigComponents could leverage it. So maybe we don't go as far as <twig:if>, but <twig:Foo> would work, and then perhaps there is then some way to "plug into" Twig's core to be notified when this syntax is found (so that the TwigComponents code could take over).

@faizanakram99
Copy link

yes please, the current syntax of twig is sometimes unreadable, for example consider this statement here, it isn't really clear when an attribute starts and when it ends, with html syntax it will be ':disabled="fieldType != 'select'"' (much easier to read)

{% if form.choices is defined %}
    {{ form_row(form.choices, {'attr': {':disabled': 'fieldType != "select"'}, 'row_attr': {'x-show': 'fieldType == "select"'} }) }}
{% endif %}

@ericmorand
Copy link
Contributor

@kbond so you propose to add the tag <twig:> to the list of official tags of the langage? Or did I misunderstood?

@kbond
Copy link
Contributor Author

kbond commented Dec 12, 2023

I think Ryan's second proposal (2) is probably the best starting point. I believe this would simplify twig components code and, assuming we get support/adoption, perhaps sell the idea of html syntax in core (Ryan's first proposal).

@ericmorand
Copy link
Contributor

What would the first proposal ("html syntax in core") mean outside of the Symfony framework? How would such syntax translate when used this way:

index.php

<?php

require './vendor/autoload.php';

$loader = new \Twig\Loader\ArrayLoader([
    'index.twig' => '<twig:if foo == true>Here<twig:else>There<twig:endif>'
]);
$env = new \Twig\Environment($loader, []);

$env->display('index.twig', []);
php index.php

@kbond
Copy link
Contributor Author

kbond commented Dec 12, 2023

IIRC, the way we have html-syntax working in twig components is: translate the html syntax to standard twig syntax, then compile down to php as normal.

I'm quite out of my depth here but I believe html-syntax in core could remove the "translate the html syntax to standard twig syntax" step. So essentially, your example would just work (assuming this new lexer is enabled by default).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants