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
View element-api.php
<?php
use craft\elements\Entry;
return [
'endpoints' => [
'search.json' => function() {
// settings
$section_handle = 'articles';
$phrase = Craft::$app->request->getParam('query');
@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 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 %}
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 ') %}
@piotrpog
piotrpog / quick_edit.twig
Last active Jul 6, 2019
Quick edit link component for Craft CMS. More info on http://craftsnippets.com/articles/quick-edit
View quick_edit.twig
{# v3 #}
{% if currentUser and currentUser.can('accessCp') and not craft.app.request.isLivePreview %}
{% set element = element|default(craft.app.urlManager.matchedElement) %}
{% if element and element.isEditable %}
{% set editLink = element.getCpEditUrl() %}
{% set editText = 'edit'|t %}
{% else %}
{% set editLink = cpUrl() %}
{% set editText = 'control panel'|t %}
{% endif %}
View breadcrumb.twig
{# v3 #}
{# 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 #}
@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 / 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 %}
You can’t perform that action at this time.