[beta2] Replace shorthand ternary operators by the default filter

Created on 14 March 2023, almost 2 years ago
Updated 17 March 2023, almost 2 years 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 DSFR:

  • templates/patterns/button/pattern-button.html.twig: {% set url = url ?: attributes.href %}
  • templates/patterns/alert/pattern-alert.html.twig:{% set title_tag = title_tag ?: 'p' %}
  • templates/patterns/alert/pattern-alert.html.twig: {{ close_title ?: 'Close'|t }}
  • templates/patterns/link/pattern-link.html.twig:{% set url = url ?: attributes.href %}
  • templates/patterns/breadcrumb/pattern-breadcrumb.html.twig: {% set url = item.url ?: item.attributes.href %}
  • templates/patterns/breadcrumb/pattern-breadcrumb.html.twig:
  • templates/patterns/accordion/pattern-accordion.html.twig: {% set title_tag = title_tag ?: 'h3' %}
  • templates/patterns/download_link_group/pattern-download-link-group.html.twig:{% set title_tag = title_tag ?: 'h4' %}
  • templates/patterns/callout/pattern-callout.html.twig:{% set title_tag = title_tag ?: 'p' %}
  • templates/patterns/callout/pattern-callout.html.twig:{% set text_tag = text_tag ?: 'p' %}
  • templates/patterns/summary/pattern-summary.html.twig:{% set title_tag = title_tag ?: 'p' %}
  • templates/patterns/summary/pattern-summary.html.twig: {% set url = link.url ?: link.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.

📌 Task
Status

Fixed

Version

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