Skip to content

Instantly share code, notes, and snippets.

@piotrpog
piotrpog / searchit_category_filter.twig
Last active February 28, 2019 22:43
Category filter for Craft Cms searchit plugin. Takes nesting of categories into account. More information on http://craftsnippets.com/articles/filtering-entries
{% set handle = 'category_handle' %}
{% for category in craft.categories.group(handle).all() %}
{% set line = '' %}
{% set depth = category.getAncestors()|length %}
{% for number in range(0, depth) %}
{% set line = line ~ '=' %}
{% endfor %}
{{ ({
filter: {
relatedTo: category.id
{% macro truncate(text, limit, append) %}
{# settings #}
{% set suffix = append|default('...') %}
{% set punctuation = ['.',',','-',':',';'] %}
{# logic #}
{% set array = text|split(' ') %}
{% set arrayTruncated = array|slice(0, limit) %}
{% set string = arrayTruncated|join(' ') %}
{% if limit and array|length > limit %}
{% for mark in punctuation %}
var gulp = require('gulp');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
const notifier = require('node-notifier');
gulp.task('dev', ['sass:development', 'watch:development', 'browser-sync']);
gulp.task('default', ['dev']);
@piotrpog
piotrpog / Craft CMS - URL macro
Last active March 4, 2019 14:26
This macro fetches routes into Twig template. More info on http://craftsnippets.com/articles/url-macro
{# v2 #}
{% macro url(routeName, tokens) %}
{% spaceless %}
{% set keyword = 'name' %}
{% set routeString = null %}
{% for route, routeSettings in craft.app.routes.getConfigFileRoutes %}
{% if routeSettings[keyword] is defined and routeSettings[keyword] == routeName %}
{% set routeString = route %}
{% endif %}
@piotrpog
piotrpog / quick_edit.twig
Last active December 3, 2023 15:59
Quick edit link component for Craft CMS 3. More info on http://craftsnippets.com/articles/quick-edit
{# v4 #}
{% 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() %}
{% if element.draftId is defined and element.draftId is not null %}
{% set editLink = editLink ~ '&draftId='~ element.draftId %}
{% endif %}
{% set editText = 'edit'|t %}
{% else %}
@piotrpog
piotrpog / app.php
Last active March 9, 2019 08:32
Config for connecting to SMTP server with Craft CMS. More info on http://craftsnippets.com/articles/testing-and-debugging-emails-sent-by-craft-cms-using-mailtrap
<?php
return [
'components' => [
'mailer' => function() {
$settings = \craft\helpers\App::mailSettings();
$settings->transportType = \craft\mail\transportadapters\Smtp::class;
$settings->transportSettings = [
'useAuthentication' => true,
'host' => getenv('SMTP_HOST'),
'port' => getenv('SMTP_PORT'),
{% spaceless %}
{# settings #}
{% set title = siteName %}
{% set channelDescription = 'description' %}
{% set descriptionHandle = 'intro' %}
{% set items = craft.entries.section('articles').limit(50) %}
{# feed #}
{% if craft.app.request.segments|last == _self %}
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<?php
use craft\elements\Entry;
return [
'endpoints' => [
'search.json' => function() {
// settings
$section_handle = 'articles';
$phrase = Craft::$app->request->getParam('query');
@piotrpog
piotrpog / breadcrumb.twig
Last active August 15, 2020 11:18
Breadcrumb template component for Craft CMS. More info on http://craftsnippets.com/articles/breadcrumb-created-from-url-for-craft-cms
{# v4 #}
{# 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 / breadcrumb_seomatic.twig
Last active August 15, 2020 11:18
Breadcrumb template component for Craft CMS based on Seomatic plugin. More info on http://craftsnippets.com/articles/breadcrumb-created-from-url-for-craft-cms
{% v2 %}
% 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>