Skip to content

Instantly share code, notes, and snippets.

@piotrpog
piotrpog / phone_number.twig
Last active June 21, 2019 13:44
Phone number macro - this macro strips unnesesary haracters from phone numbers and generates "phone links.
{% macro phoneNumber(phoneNumber) %}
<a href="tel:{{ phoneNumber|replace({'-':'', ' ':'', '.':'', '(':'', ')':''}) }}">{{ phoneNumber }}</a>
{% endmacro %}
@piotrpog
piotrpog / read_time_macro.twig
Created June 21, 2019 21:22
This macro will return read time in human readable format, adjusted to your site locale. More info: http://craftsnippets.com/articles/read-time-macro-for-craft-cms
{% macro readTime(text) %}
{% spaceless %}
{# v1 #}
{# settings #}
{% set wordsPerMinute = 265 %}
{% set showLessThanMinute = true %}
{# logic #}
{% set plaintext = text|striptags|replace({'—': ' ', '–': ' ', '-': ' ', '\n': ' '}) %}
{% set words = plaintext|split(' ') %}
{% set minutes = ceil(words|length / wordsPerMinute) %}
@piotrpog
piotrpog / pagination_ellipsis.twig
Last active June 24, 2019 15:54
Pagination template component for Craft CMS. Excessive links are replaced by elipsis. More info: http://craftsnippets.com/articles/ellipsis-pagination-component-for-craft-cms
{# v3 #}
{# http://craftsnippets.com/articles/ellipsis-pagination-component-for-craft-cms #}
{# requires pageInfo variable #}
{# settings #}
{% set neighbours = 1 %}
{# symbols #}
{% set prev = '&#10094;' %}
{% set next = '&#10095;' %}
{# v1 #}
{% macro truncateWords(text, length, suffix) %}
{% spaceless %}
{# settings #}
{% set defaultSuffix = '...' %}
{# logic #}
{% if text and length %}
{% set suffix = suffix|default(defaultSuffix) %}
{% set text = text|striptags %}
{% set text = text|replace('/\\n/', '\n ') %}
{# v1 #}
{% macro truncateChars(text, limit, suffix) %}
{% spaceless %}
{# settings #}
{% set defaultSuffix = '...' %}
{# logic #}
{% if text and limit %}
{% set text = text|striptags %}
{% set suffix = suffix|default(defaultSuffix) %}
{% set stringy = create(
<?php
use craft\elements\Entry;
return [
'endpoints' => [
'search.json' => function() {
// settings
$section_handle = 'articles';
$phrase = Craft::$app->request->getParam('query');
@piotrpog
piotrpog / yt_iframe.twig
Last active August 30, 2019 21:40
Twig macro making embedded youtube players responsive and lazy-loaded. Requires Craft CMS Retcon plugin, More info: http://craftsnippets.com/articles/responsive-and-lazy-loaded-youtube-videos-with-craft-cms
{# v2 #}
{%- macro ytIframe(html, lazy = true) -%}
{% if html is not empty and craft.app.plugins.isPluginEnabled('retcon') %}
{% set html = html|retconAttr('iframe', {'style' : 'position: absolute; top: 0; left: 0; width: 100%; height: 100%;'} ) %}
{% if lazy %}
{% set html = html|retconAttr('iframe', {'loading' : 'lazy'} ) %}
{% endif %}
{% set html = html|retconWrap( [ 'iframe' ], 'figure') %}
{% set html = html|retconAttr('figure', {'style' : 'position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin: 0px;'} ) %}
{% set html = html|retconWrap( [ 'figure' ], 'div.youtube-player') %}
@piotrpog
piotrpog / time_ago.twig
Last active September 9, 2019 05:55
Macro returning elapsed time in human-readable format. More info: http://craftsnippets.com/articles/working-with-dates-in-craft-cms-templates
{%- macro timeAgo(date, additionalAttributes) -%}
{# v1 #}
{% if date is defined %}
{# settings #}
{% set format = 'medium' %}
{% set locale = currentSite.language %}
{# logic #}
{% set formatter = create({ class: 'craft\\i18n\\Formatter', locale: locale }) %}
{% set attributes = {
text: formatter.asRelativeTime(date),
{%- 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 craft.app.urlManager.matchedElement and craft.app.urlManager.matchedElement.uri == '__home__' %}
{% set seoTitle = craft.app.urlManager.matchedElement.title %}
{% elseif craft.app.urlManager.matchedElement %}
{% set seoTitle = craft.app.urlManager.matchedElement.title ~ ' - ' ~ siteName %}
{% else %}
{% set seoTitle = siteName %}
{% endif %}
<title>{{ seoTitle }}</title>