twigphp / twigphp/Twig

Add support for a null-safe filter operator ?|

Open
#4,788 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
8.4k
Forks
1.3k
Avg merge
1d 13h
Merged PRs (30d)
37

Description

My twig templates are full of code where I have a parameter, usually a date, that I need to display if it exists:

{{ site.startDate ? site.startDate|date('m/d/Y') }}

It would be nice if this could be shortened to something like this:

{{ site.startDate ?| date('m/d/Y') }}

I know that I could just write a a custom date filter that handles null input, along the lines of below, but this seems a bit much:

#[AsTwigFilter(name: 'safe_date', needsEnvironment: true)]
    public function safeDate(Environment $env, $date, ?string $format = null, $timezone = null): string
    {
        if (empty($date)) {
            return '';
        }
        return $env->getExtension(CoreExtension::class)->formatDate($date, $format, $timezone);
    }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or entry point is named in the issue. Start by locating Twig's expression and filter syntax handling, then define and test the proposed null-safe ?| behavior using the examples; done means null values are handled as requested without breaking existing filter syntax.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.