Skip to content

Instantly share code, notes, and snippets.

@piotrpog
Last active September 9, 2019 05:55
Show Gist options
  • Save piotrpog/d106c79323ab6c4d6131a1dc752ec0d6 to your computer and use it in GitHub Desktop.
Save piotrpog/d106c79323ab6c4d6131a1dc752ec0d6 to your computer and use it in GitHub Desktop.
Macro for setting default date format. More info: http://craftsnippets.com/articles/working-with-dates-in-craft-cms-templates
{%- macro defaultDateFormat(date, additionalAttributes) -%}
{# v1 #}
{% if date is defined %}
{# settings #}
{% set format = 'medium' %}
{# logic #}
{% set attributes = {
text: date|date(format),
datetime: date|date('yy-m-d')
} %}
{% if additionalAttributes is defined and additionalAttributes is not iterable %}
{% set attributes = attributes|merge({class: additionalAttributes}) %}
{% elseif additionalAttributes is iterable %}
{% set attributes = attributes|merge(additionalAttributes) %}
{% endif %}
{{tag('time',attributes)}}
{% endif %}
{%- endmacro -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment