Skip to content

[TwigComponent] make public component properties available directly #232

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
Jan 21, 2022

Conversation

kbond
Copy link
Member

@kbond kbond commented Jan 20, 2022

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

This makes all public properties available directly in your component template. Assuming your component has a public property foo:

{# instead of #}
{{ this.foo }}

{# you can use #}
{{ foo }}

@kbond kbond requested a review from weaverryan January 20, 2022 17:53
@kbond kbond force-pushed the feature/public-component-props branch from 2a67668 to c3569da Compare January 20, 2022 18:22
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.

This seems very practical. There are a few more examples in the TwigComponent docs and many examples in LiveComponents. I think we should lean in and prefer the short version 90%+ of the time.

@kbond kbond force-pushed the feature/public-component-props branch from c3569da to 964b9bd Compare January 21, 2022 15:18
@kbond
Copy link
Member Author

kbond commented Jan 21, 2022

Rebased to pickup the new doc format and I think I updated all the applicable this. calls in the docs.

@weaverryan
Copy link
Member

Thanks Kevin!

@weaverryan weaverryan merged commit 9f1ec06 into symfony:2.x Jan 21, 2022
@kbond kbond mentioned this pull request Feb 1, 2022
1 task
weaverryan added a commit that referenced this pull request Feb 14, 2022
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[Twig] add `ExposeInTemplate` attribute

| 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:

```php
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:**
- [x] use to expose `form` in `ComponentWithFormTrait` (requires #254)

Commits
-------

e6c53fd [Twig] add `ExposeInTemplate` attribute
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