2017 © Pedro Peláez
 

library twig-safe-date

A safe date function in twig that will not set null dates to today.

image

vivait/twig-safe-date

A safe date function in twig that will not set null dates to today.

  • Friday, October 27, 2017
  • by Brunty
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,307 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 19 % Grown

The README.md

Twig Safe Date Extension

Simple filter in twig to help avoid problems with things like: {{ null|date('Y-m-d') }}, (*1)

There are ways around it using twig - the documentation even states:, (*2)

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:, (*3)

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

But often, this is overlooked., (*5)

Requirements

  • PHP >=7.4 or >= 8.2
  • Twig ^3.0

Installation & Usage

composer require vivait/twig-safe-date, (*6)

Once required, register the extension with Twig:, (*7)

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

Once registered, you can use the filter date to output dates as before, with the change that: null values will get turned into known content (default: -) rather than today's date, (*8)

The default format is 'F j, Y H:i.' (the same as the core date filter in Twig), (*9)

{{ post.posted_at|date }}

If you wish to change the format of the date, pass it a parameter with your preferred format:, (*10)

{{ post.posted_at|date("d/m/Y") }}

If post.posted_at is null, then by default the filter will output -, if you wish to change this to a different value, pass a new default as the third parameter:, (*11)

{{ post.posted_at|date("d/m/Y", "Europe/London" "Content if null") }}

The Versions

27/10 2017

dev-master

9999999-dev

A safe date function in twig that will not set null dates to today.

  Sources   Download

MIT

The Requires

 

The Development Requires

27/10 2017

0.1

0.1.0.0

A safe date function in twig that will not set null dates to today.

  Sources   Download

MIT

The Requires

 

The Development Requires