Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if function/extension is defined

In my Symfony2 bundle, I need to check if a function (an extension) is defined. More specifically, if the KnpMenuBundle is installed I use that one in my bundle, otherwise I will render the plugin myself.

I tried this, but this of course doesn't work:

{% if knp_menu_render() is defined %}
    {# do something #}
{% else %}
    {# do something else #}
{% endif %}

Is there a test/function/filter to check if a function is defined? If not, is there another way to check if the KnpMenuBundle is installed in the application?

like image 651
Wouter J Avatar asked Oct 05 '22 13:10

Wouter J


1 Answers

Write a function in a Twig Extension which would check if bundle is enabled.

List of registered bundles is stored in the kernel.bundles parameter.

Twig extensions are registered as services. This means you can pass any other service or a parameter to your extension:

<services>
    <service id="acme.twig.acme_extension" class="Acme\DemoBundle\Twig\AcmeExtension">

        <argument>%kernel.bundles%</argument>

        <tag name="twig.extension" />
    </service>
</services>

In your Twig function or filter you can later use a service or a parameter which you've passed to an extension.

like image 180
Jakub Zalas Avatar answered Oct 13 '22 12:10

Jakub Zalas