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

add component to guard tag #4390

Closed
tacman opened this issue Oct 10, 2024 · 7 comments
Closed

add component to guard tag #4390

tacman opened this issue Oct 10, 2024 · 7 comments

Comments

@tacman
Copy link

tacman commented Oct 10, 2024

I love the guard tag, and was wondering if it could be applied to twig components, too, so that something like this would work:

{% guard component 'bootstrap:accordion' %}
<twig:bootstrap:accordion />
{% else %}
{# dump data as paragraphs #}
{% endif %}

I put the issue in the ux repo, but perhaps it belongs here.

symfony/ux#2255

@stof
Copy link
Member

stof commented Oct 10, 2024

given that Twig itself knows nothing about twig components (which are a concept of symfony/ux, not of Twig), I don't think it can be added in the core tag.

However, from a quick look at the implementation of TwigComponent in symfony/ux, I don't have the impression that they validate component names are compile time. If this is indeed a runtime check, a {% if component_exists('bootstrap:accordion') %} would work (assuming they define such a component_exists function), or an alternative {% if 'bootstrap:accordion' is component %} if they prefer exposing a test. anyway, such choice would belong to the symfony/ux repo.

@tacman
Copy link
Author

tacman commented Oct 10, 2024

Yeah, that's why I opened it there. But the original PR mentioned something like "currently the guard tags supports filters and functions", it made me think perhaps where was a way a 3rd-party component could "attach" itself to it.

But a component_exists() function seems like an easier solution anyway.

@stof
Copy link
Member

stof commented Oct 10, 2024

there is not hook for third-party extensions in it (which would not be possible anyway, as the implementation relies on checking for that filter/function/test on the Environment instance).

@tacman
Copy link
Author

tacman commented Oct 10, 2024

thanks for the explanation!

@tacman tacman closed this as completed Oct 10, 2024
@smnandre
Copy link
Contributor

A guard can be used at a higher level to check whether symfony/ux-twig-component is installed or not.

{% guard function component %}
    <twig:bootstrap:accordion />
{% endguard %}

@tacman
Copy link
Author

tacman commented Oct 12, 2024

@smnandre yes, but it'd also be nice to check if a specific component was installed.

{% set count = 15 %}
{% guard function component %}
    {% if (component_exists('bootstrap:badge') %}
        <twig:bootstrap:badge :message="count"  />
   {% else %}
        {# badge is in my bundle if ux-component is installed, so I don't need to check for it #}
        <twig:my-bundle:badge :message="count"  />
   {% endif %}
{% else %}
       <span class="badge">{{ count }}</span>
{% endguard %}

@smnandre
Copy link
Contributor

I know I Even answered you about this on the UX repository :)

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

No branches or pull requests

3 participants