Replace shorthand ternary operators by the default filter

Created on 14 March 2023, almost 2 years ago
Updated 27 March 2023, over 1 year ago

Problem/Motivation

Short ternary operator syntax can be used by leaving out the middle part of the ternary operator for quick shorthand evaluation:

expression1 ?: expression2

We use it many time in bootstrap 4:

  • templates/patterns/list/pattern-list.html.twig:{% set list_type = list_type ?: 'ul' %}
  • templates/patterns/button/pattern-button.html.twig: {% set url = url ?: attributes.href %}
  • templates/patterns/badge/pattern-badge.html.twig: {% set url = url ?: attributes.href %}
  • templates/patterns/pagination/pattern-pagination.html.twig: {% set attributes = attributes ?: create_attribute() %}
  • templates/patterns/list_group_item/pattern-list_group_item.html.twig: {% set url = url ?: attributes.href %}

And bootstrap 5:

  • templates/patterns/list/pattern-list.html.twig:{% set list_type = list_type ?: 'ul' %}
  • templates/patterns/button/pattern-button.html.twig: {% set url = url ?: attributes.href %}
  • templates/patterns/pagination/pattern-pagination.html.twig: {% set attributes = attributes ?: create_attribute() %}
  • templates/patterns/list_group_item/pattern-list-group-item.html.twig: {% set url = url ?: attributes.href %}

This operator is competing with the default filter

expression1|default(expression2)

And it seems default filter is a better solution:

Proposed resolution

Replace shorthand ternary operators by default filter.

For 4.x, push directly the commit. For 5.x, do a MR.

📌 Task
Status

Fixed

Version

5.0

Component

Code

Created by

🇫🇷France pdureau Paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024