vscode-twig-language icon indicating copy to clipboard operation
vscode-twig-language copied to clipboard

Default value support for macro arguments

Open hansgrinwis opened this issue 2 years ago • 2 comments

Hello junstyle,

Thank you for this extension. When writing this piece of code:

{% macro menuLink(href, config, active = false, class = '') %}

the extension reports the following "problem":

ERROR: Expected comma or ")". The argument list of a macro can only consist of parameter names separated by commas.

but Twig supports default values for arguments nowadays.

Can this be fixed?

hansgrinwis avatar Aug 30 '23 18:08 hansgrinwis

@hansgrinwis in the meantime, i believe you can write this using the default() filter inside your macro:

{% macro menuLink(href, config, active, class) %}
  <a href="{{ href }}" class="{{ active|default(false) ? 'active' }} {{ class|default('') }}">{{ config.label }}</a>
{% endmacro %}

Obviously i’m assuming here that you have an object for config with a label key, but the rest of the code should match with your case and work.

furioursus avatar Oct 20 '23 16:10 furioursus

@furioursus That's a nice workaround. Thank you!

hansgrinwis avatar Oct 24 '23 19:10 hansgrinwis