Skip to content

[Twig] add ExposeInTemplate attribute #259

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

Merged
merged 1 commit into from
Feb 14, 2022

Conversation

kbond
Copy link
Member

@kbond kbond commented Feb 1, 2022

Q A
Bug fix? no
New feature? yes
Tickets n/a
License MIT

#232 made all of a component's public properties available directly (without the this.) in it's template. This adds an ExposeInTemplate attribute to make non-public properties available in this way.

Example:

use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;

#[AsTwigComponent('with_exposed_variables')]
final class WithExposedVariables
{
    #[ExposeInTemplate]
    private string $prop1; // available as `{{ prop1 }}`

    #[ExposeInTemplate('customProp')]
    private string $prop2; // available as `{{ customProp }}`

    #[ExposeInTemplate(name: 'ico', getter: 'fetchIcon')]
    private string $icon = 'ico-warning'; // available as `{{ ico }}` in the template using `fetchIcon()` as the getter

    /**
     * Required to access $prop1
     */
    public function getProp1(): string
    {
        return $this->prop1;
    }

    /**
     * Required to access $prop1
     */
    public function getProp2(): string
    {
        return $this->prop2;
    }

    /**
     * Required to access $this->icon
     */
    public function fetchIcon(): string
    {
        return $this->icon;
    }
}

TODO:

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@kbond
Copy link
Member Author

kbond commented Feb 4, 2022

What do you think about making this a property-only attribute? Then for methods, we'll recommend either using this. or computed. (from #266)? I think this could help with documentation.

For making form available on ComponentWithFormTrait, I'm thinking we could use this syntax:

trait ComponentWithFormTrait
{
    #[ExposeInTemplate(name: 'form', getter: 'getForm')]
    private ?FormView $formView = null;

    // ...
}

We wouldn't have to rename $formView. The getter attribute argument wouldn't make sense for methods.

@weaverryan
Copy link
Member

What do you think about making this a property-only attribute? Then for methods, we'll recommend either using this. or computed. (from #266)? I think this could help with documentation

Hmm, ok. Let's do this. It doesn't prevent us from adding this later by popular demand, so I don't see any risk. 👍

@kbond kbond force-pushed the expose-in-template branch 2 times, most recently from 92ca639 to 7754b34 Compare February 11, 2022 15:14
@kbond
Copy link
Member Author

kbond commented Feb 11, 2022

I believe this to be ready.

@weaverryan
Copy link
Member

Thank you Kevin - looks awesome!

@weaverryan weaverryan merged commit 2e25ba3 into symfony:2.x Feb 14, 2022
@kbond kbond deleted the expose-in-template branch February 14, 2022 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants