Seif Sayed
Seif Sayed

Reputation: 803

Twig null date filter shows current date

According to the documentation

If the value passed to the date filter is null, it will return the current date by default. If an empty string is desired instead of the current date, use a ternary operator:

http://twig.sensiolabs.org/doc/2.x/filters/date.html

The problem is the solution provided entails that we revisit all dates in the application and apply the ternary operation as we never want to show today's date instead of null.

is it possible to override the default date filter? if so how can I implement this. We're using twigs with symfony 2.7

Upvotes: 10

Views: 6555

Answers (3)

user1077915
user1077915

Reputation: 894

From the documentation:

If the value passed to the date filter is null, it will return the current date by default. If an empty string is desired instead of the current date, use a ternary operator:

{{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}

You can check it at https://twig.symfony.com/doc/3.x/filters/date.html

Upvotes: 10

B3none
B3none

Reputation: 409

Here's the Twig 3.0 solution

Extension class:

namespace Application\Twig\Extensions;

use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class DateWithFallback extends AbstractExtension
{
    /**
     * @var Environment
     */
    protected $twig;

    /**
     * DateWithFallback constructor.
     *
     * @param Environment $twig
     */
    public function __construct(Environment $twig)
    {
        $this->twig = $twig;
    }

    /**
     * @return array|TwigFilter[]
     */
    public function getFilters(): array
    {
        return [
            new TwigFilter('date', [$this, 'dateFilter']),
        ];
    }

    /**
     * @param string|null $timestamp
     * @param string $fallback
     * @param string $format
     * @return string
     */
    public function dateFilter(?string $timestamp, string $fallback = 'Not set', string $format = 'd/m/Y'): string
    {
        if ($timestamp !== null) {
            return twig_date_format_filter($this->twig, $timestamp, $format);
        }

        return $fallback;
    }
}

Adding the extension assuming that $this->twig is your Twig Environment:

$this->twig->addExtension(new DateWithFallback($this->twig));

Upvotes: 4

Veve
Veve

Reputation: 6758

As explained here in the doc, you can override an existing filter:

To overload an already defined filter, test, operator, global variable, or function, re-define it in an extension and register it as late as possible (order matters).

Here is the code to return an empty string instead of the current date if null:

class DateEmptyIfNull extends Twig_Extension
{
    public function getFilters()
    {
        return array(
            new Twig_Filter('date', array($this, 'dateFilter')),
        );
    }

    public function dateFilter($timestamp, $format = 'F j, Y H:i')
    {
        $result = '';
        if($timestamp !== null)
        {
            $result = parent::dateFilter($timestamp, $format);
        }
        return $result;
    }
}

$twig = new Twig_Environment($loader);
$twig->addExtension(new DateEmptyIfNull());

Upvotes: 12

Related Questions