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

Whitespace control when using twig components? #2228

Open
nicolas-grekas opened this issue Oct 2, 2024 · 6 comments
Open

Whitespace control when using twig components? #2228

nicolas-grekas opened this issue Oct 2, 2024 · 6 comments
Labels

Comments

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Oct 2, 2024

I'm using a twig component like:

<twig:Turbo:Stream:Append target="#some-id">
    <div>Append this</div>
</twig:Turbo:Stream:Append>

But the whitespace around the div is noise (but still nice for formatting).
Is there a way to ask twig to remove it?
Using the non-HTML-like syntax, I can by using -%} / {%-, but what about the html-like syntax?
Is there some twig:trim="before/after/true/both" that I could use for that? Or that could be added if that looks like a good idea?

<twig:Turbo:Stream:Append target="#some-id" twig:trim="true">
    <div>Append this</div>
</twig:Turbo:Stream:Append>
@Kocal
Copy link
Member

Kocal commented Oct 2, 2024

Hi Nicolas,

But the whitespace around the div is noise

What do you mean about "noise"? is it blocking Turbo or equivalent? or is it "purely visual" ?

When I look at the examples they have spaces around the first element, and it seems to be working well. IIUC Turbo will find the first <template> element inside <turbo-stream>, so whitespaces does not matter here.

Is there some twig:trim="before/after/true/both" that I could use for that?

For the moment no, it's not possible to control whitespaces for the component HTML syntax.

Or that could be added if that looks like a good idea?

It can yes, I think there's surely a use case where it would be desirable to control blanks, to avoid the following problem:

image

About the implementation, maybe twig:trim can be added yes, but this attribute should be intercepted and removed from the attributes passed to the Component instance.
I've also thought about <twig:Foo-> and <-/twig:Foo>, it definitely looks weird, but it can remind you about Twig -%} and {%-.

@nicolas-grekas
Copy link
Member Author

Most of the time it doesn't matter, sometimes it does :) eg the :empty pseudo-CSS class requires no spaces.
Maybe there is nothing to do and ppl should use either the non-html syntax or remove the whitespace themselves.

@Kocal
Copy link
Member

Kocal commented Oct 2, 2024

Ah, yeah, didn't think about :empty pseudo-selector, you're right!

I also thought about globally configure this behaviour, like vue-template-compiler used to do with Vue 2 and the compiler option whitespace, but it looks like it has been removed with Vue 3 and @vue/compiler-sfc.
It looks like such an option does not exist with React and Svelte, but it does on Angular 18

Some folks in sveltejs/svelte#3080 suggested about a :trim attribute, maybe that's the best option.

@nicolas-grekas
Copy link
Member Author

:trim could be nice as a generic Twig feature on its own!

@smnandre
Copy link
Member

smnandre commented Oct 5, 2024

This should be dealt with in Twig i think.

But you can already enforce it inside your component i suppose ?

{# index.html.twig  #}

<twig:Acme:Alert>
    
    
    
    Hahaa
    
</twig:Acme:Alert>
{# Acme:Alert.html.twig #}
<div{{ attributes }}>



{%- block content '' -%}

</div>

Should render <div>Hahaa</div> no ?

In general i'm seeing TwigComponent as "blocks" more than "elements", and so internal whitespace should not be something to consider too much, but i'm open to any change if that is something people want/need.

Just: no new exotic syntax for now please ^^

@smnandre
Copy link
Member

smnandre commented Oct 5, 2024

@Kocal

span { display: inline; }

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

No branches or pull requests

3 participants