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 / favicons.twig
Created Jul 16, 2019
Twig component generating favicons from files uploaded trough control panel. More info: http://craftsnippets.com/articles/adding-favicons-to-craft-cms-website
View favicons.twig
{# v1 #}
{# requires global with handle 'favicon' containing asset field with handle 'faviconFile' #}
{% cache globally %}
{% set sizesIcon = [192, 32, 16] %}
{% set sizesAppleTouch = [180] %}
{% if favicon is defined and favicon['faviconFile'] is defined and favicon.faviconFile.exists() and favicon.faviconFile.one().kind == 'image' %}
{# link icon #}
{% for faviconSize in sizesIcon %}
{% set icon = favicon.faviconFile.one() %}
{% set shorterEdge = icon.width > icon.height ? icon.height : icon.width %}
View truncate_characters.twig
{# 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(
View truncate_words.twig
{# 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 ') %}
View language_switcher.twig
{# v2 #}
{# http://craftsnippets.com/articles/universal-language-switcher-for-craft-cms #}
{# logic #}
{% set currentElement = craft.app.urlManager.matchedElement %}
{% set sites = craft.app.getSites().getGroupById(currentSite.groupId).getSites() %}
{% set switcherLinks = [] %}
{% for site in sites if site.baseUrl is not empty %}
@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 %}
You can’t perform that action at this time.