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

[TwigComponent]: allow to configure template naming strategy #2136

Open
richardhj opened this issue Sep 8, 2024 · 3 comments
Open

[TwigComponent]: allow to configure template naming strategy #2136

richardhj opened this issue Sep 8, 2024 · 3 comments

Comments

@richardhj
Copy link
Contributor

You can configure the templates as follows:

# config/packages/twig_component.yaml
twig_component:
    anonymous_template_directory: 'components/'
    defaults:
        # Namespace & directory for components
        App\Twig\Components\: 'components/'

which translates to: all Components in the PHP namespace App\Twig\Components\ will have their templates located in 'components/', so App\Twig\Components\MyModal will become components/MyModal.html.twig.

As some frameworks prefer snake_case over PascalCase, I'd like to configure the naming strategy as follows:

# config/packages/twig_component.yaml
twig_component:
    anonymous_template_directory: 'components/'
    defaults:
        # Namespace & directory for components
        App\Twig\Components\:
          template_directory: 'components/'
          name_prefix: ''
          naming_strategy: 'snake_case'

so App\Twig\Components\MyModal will become components/my_modal.html.twig.

@richardhj richardhj added the RFC label Sep 8, 2024
@richardhj richardhj changed the title [TwigComponent]: allow to configure template naming structure [TwigComponent]: allow to configure template naming strategy Sep 8, 2024
@smnandre
Copy link
Member

smnandre commented Sep 8, 2024

To be sure I understood perfectly your suggestion: are you talking about "template naming", "component naming" or both ?

For "template naming", losely based on your example, something like this:

# config/packages/twig_component.yaml
twig_component:
    defaults:
        App\Twig\Components\: components/
       App\Twig\Components\Acme:
          template_directory: 'components/acme/'
          name_prefix: 'Acme'
          naming_strategy: 'snake_case'   

Then this class

// src/Twig/Components/Acme/FooBar.php

namespace App\Twig\Components\Acme;

#[AsTwigComponent]
class FooBar
{
    // ... 
}

would be equivalent to

#[AsTwigComponent(
    template: 'components/acme/foo_bar.html.twig',
]
class FooBar
{

And

# Twig syntax
{{ component('Acme:FooBar', { ... }) }} 
# will use 'components/acme/foo_bar.html.twig' during render

# HTML syntax
<twig:Acme:FooBar />
# will use 'components/acme/foo_bar.html.twig' during render

Or did you imply the entire class Acme\FooBar should be registered as a component named acme:foo_bar ?

Or, to be more precise, any component or template in the Acme "namespace" should be registered in lower-case / snake case ?

<twig:Acme:foo_bar />
# will use 'components/acme/foo_bar.html.twig' during render

As you used the name_prefix: 'Acme' i'm not sure and, depending it could open very different questions :)

@richardhj
Copy link
Contributor Author

Then this class

// src/Twig/Components/Acme/FooBar.php

namespace App\Twig\Components\Acme;

#[AsTwigComponent]
class FooBar
{
    // ... 
}

would be equivalent to

#[AsTwigComponent(
    template: 'components/acme/foo_bar.html.twig',
]
class FooBar
{

Or did you imply the entire class Acme\FooBar should be registered as a component named acme:foo_bar ?

Yes, only the templates :) your first example is totally right and my expected behavior :-)

As you used the name_prefix: 'Acme' i'm not sure and, depending it could open very different questions :)

name_prefix is already a config option, no? Should be totally unrelated to this issue :)

@smnandre
Copy link
Member

smnandre commented Sep 8, 2024

Should be totally unrelated to this issue :)

Yes that's was how i read it, but wanted to be sure.. as if you wanted to change the "name" of the component (not only the template one), it could be something strange to configure component to be named "Acme:foo_bar" :)

Yes, only the templates :) your first example is totally right and my expected behavior :-)

Ok, so I'd suggest we call this potential option: template_naming_strategy.

Something like this ?

# config/packages/twig_component.yaml

    # ... 

    # default: null 
    template_naming_strategy:  
    # (equivalent to)
    template_naming_strategy:  PascalCase

    # new option
    template_naming_strategy:  snake_case

What we need to see now is the impact on other things (anonymous, live, etc..) and check for any unwanted side effect.

For instance, this will not work for anonymous component (as there is no name to convert), right ?

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

No branches or pull requests

3 participants