junstyle / vscode-twig-language

VS Code extension with snippets, syntax highlighting, hover, and formatting for Twig.
https://marketplace.visualstudio.com/items?itemName=junstyle.twig-language
MIT License
16 stars 2 forks source link

Default value support for macro arguments #5

Open hansgrinwis opened 1 year ago

hansgrinwis commented 1 year ago

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?

furioursus commented 11 months ago

@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.

hansgrinwis commented 11 months ago

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