symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
853 stars 315 forks source link

[TwigComponent] Html component syntax and whitespace control #849

Closed norkunas closed 1 year ago

norkunas commented 1 year ago

With the old syntax like {{- component() -%} and {%- component .. -%} it is possible to control whitespace but with the html syntax I don't see a way.

What are the options? :)

kbond commented 1 year ago

Is there a non-aesthetic reason for controlling whitespace?

norkunas commented 1 year ago

saving bytes :)

AmProsius commented 1 year ago

As a workaround you can use the spaceless filter:

{% apply spaceless %}
    <twig:Component>
        <div>Some text</div>
    </twig:Component>
{% endapply %}
kbond commented 1 year ago

saving bytes :)

I don't think it's an issue if your web server is using content encoding like gzip/brotli.

From https://symfony.com/blog/better-white-space-control-in-twig-templates:

Controlling white spaces is very important when using Twig to generate contents like YAML, text emails or any other format where the white spaces are significant.

In contrast, when generating HTML contents, most of the times you should ignore this feature, because the HTML contents are minified and compressed before sending them to the users, so trying to generate perfectly aligned HTML code is just a waste of time.

norkunas commented 1 year ago

What about emails? My point is not to align or something but squeeze as possible :)

kbond commented 1 year ago

Yeah, not sure about emails. I just remember reading that blog post and never bothering with spaceless in HTML again.

weaverryan commented 1 year ago

My thinking is this:

{% apply spaceless %}
    <twig:Component>
        <div>Some text</div>
    </twig:Component>
{% endapply %}

I'm not excited about adding a <-twig:Component-> type of thing :)

norkunas commented 1 year ago

I'm not excited about adding a <-twig:Component-> type of thing :)

Me too :) strange that when {% apply spaceless %} is used in base template it sometimes still not enough and needs to be done in nested templates