Skip to content

Instantly share code, notes, and snippets.

piotrpog

Block or report user

Report or block piotrpog

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@piotrpog
piotrpog / read_time_macro.twig
Created Jun 21, 2019
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
View read_time_macro.twig
{% 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 / phone_number.twig
Last active Jun 21, 2019
Phone number macro - this macro strips unnesesary haracters from phone numbers and generates "phone links.
View phone_number.twig
{% macro phoneNumber(phoneNumber) %}
<a href="tel:{{ phoneNumber|replace({'-':'', ' ':'', '.':'', '(':'', ')':''}) }}">{{ phoneNumber }}</a>
{% endmacro %}
@piotrpog
piotrpog / responsive_image.twig
Created Jun 19, 2019
Responsive image macro
View responsive_image.twig
{% macro responsiveImage(file, sizes, options) %}
{# settings #}
{% set pixel = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==' %}
{# logic #}
{% if file is defined and sizes is defined and sizes is iterable %}
{% if sizes|first is iterable or sizes|first is null %}
{# array of transforms #}
<picture class="{{options.class ?? null}}">
{% for media, settings in sizes %}
{% set url = settings is not null ? file.getUrl(settings) : pixel %}
View dynamic_pagination.twig
{# v2 #}
{% if pagination_list is defined %}
{% js %}
// AJAX REQUEST DATA
{% set current_url = craft.request.getRequestUri()|split(craft.request.getPath())[0]~craft.request.getPath() %}
{% set ajax_data = {
current_url: current_url,
pagination_list: pagination_list|hash,
pagination_parameters: pagination_parameters ?? null,
View disqus.twig
{% set shortname = 'YOUR_WEBSITE_SHORTNAME' %}
{% if entry is defined %}
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
this.page.url = '{{entry.url}}';
this.page.identifier = '{{entry.id}}';
this.page.title = '{{entry.title}}';
};
(function() { // DON'T EDIT BELOW THIS LINE
@piotrpog
piotrpog / pagination_select.twig
Created May 10, 2019
Pagination template component for Craft CMS. List of pages is rendered using <select> HTML element. More info: http://craftsnippets.com/articles/ellipsis-pagination-component-for-craft-cms
View pagination_select.twig
{# settings #}
{% set info = pageInfo %}
{# logic #}
{% if info.totalPages > 1 %}
<select name="" id="" onchange="document.location.href = this.options[this.selectedIndex].value;">
{% for link in info.getRangeUrls( 1, info.totalPages ) %}
<option value="{{link}}" {{info.currentPage == loop.index ? 'selected="selected"'}}>{{'page'|t}} {{loop.index}}</option>
{% endfor %}
</select>
{% endif %}
@piotrpog
piotrpog / pagination_ellipsis.twig
Last active Jun 24, 2019
Pagination template component for Craft CMS. Excessive links are replaced by elipsis. More info: http://craftsnippets.com/articles/ellipsis-pagination-component-for-craft-cms
View pagination_ellipsis.twig
{# 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;' %}
@piotrpog
piotrpog / emailLinks.twig
Created Apr 25, 2019
Twig macro converting email addresses in text into links secured from spambots. More info: http://craftsnippets.com/articles/converting-email-addresses-into-links-using-twig-macro
View emailLinks.twig
@piotrpog
piotrpog / breadcrumb_seomatic.twig
Last active Apr 14, 2019
Breadcrumb template component for Craft CMS based on Seomatic plugin. More info on http://craftsnippets.com/articles/breadcrumb-created-from-url-for-craft-cms
View breadcrumb_seomatic.twig
{% v1 %}
% set breadcrumbLinks = seomatic.jsonLd.get('breadcrumbList').itemListElement %}
{% if breadcrumbLinks|length > 1 %}
<nav class="breadcrumb" aria-label="{{'breadcrumbs'|t}}">
<ul>
{% for link in breadcrumbLinks %}
<li class="{{loop.last ? 'is-active'}}" {{loop.last ? 'aria-current="page"'}}>
<a href="{{ link.item['@id'] }}">
<span itemprop="name">{{ link.item['name'] }}</span>
View breadcrumb.twig
{# v2 #}
{# http://craftsnippets.com/articles/breadcrumb-created-from-url-for-craft-cms #}
{# settings #}
{% set nonElementLinks = false %}
{# populate breadcrumbLinks array if no array of links was provided #}
{% if breadcrumbLinks is not defined %}
{% set breadcrumbLinks = [] %}
{# home #}
You can’t perform that action at this time.