Skip to content

Instantly share code, notes, and snippets.

{% if article is defined %}
<li>
{{article.id}} - {{article.title}}
</li>
{% endif %}
{# v3 #}
{# http://craftsnippets.com/articles/universal-language-switcher-for-craft-cms #}
{# logic #}
{% set currentElement = craft.app.urlManager.matchedElement %}
{% set sites = craft.app.sites.getGroupById(currentSite.groupId).getSites() %}
{% set switcherLinks = [] %}
{% for site in sites|filter(x => x.baseUrl is not empty) %}
{% set title = craft.app.i18n.getLocaleById(site.language).displayName %}
{% set url = site.getBaseUrl() %}
{# 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 / quick_edit_craft4.twig
Last active December 10, 2023 00:45
Quick edit link component for Craft CMS 4. More info on http://craftsnippets.com/articles/quick-edit
{# v5 #}
{% if currentUser and currentUser.can('accessCp') and not craft.app.request.isLivePreview %}
{% if _globals.get('currentElement') %}
{% set element = _globals.get('currentElement') %}
{% else %}
{% set element = craft.app.urlManager.matchedElement %}
{% endif %}
{% if element and element.canSave(currentUser) and element.getCpEditUrl() is not null %}
{% set editLink = element.getCpEditUrl() %}
{% set editText = 'edit'|t %}
@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 %}
Garnish.on(Craft.BaseElementIndex, 'updateElements', (ev) => {
// ev.target = the element index
});
@piotrpog
piotrpog / product.twig
Last active June 15, 2022 19:01
Commerce product page with sprig. Old version, doesnt work with Craft 4.
{% extends 'shop/_layouts/main' %}
{% block main %}
{{ sprig('product_content', {productId: product.id}) }}
{{ sprig.script }}
{% endblock %}
@piotrpog
piotrpog / pretty_url_macro.twig
Created August 28, 2020 06:49
Pretty URL Twig Macro
{% macro prettyUrl(url) %}
{{url
|trim('https://', 'left')
|trim('http://', 'left')
|trim('www.', 'left')
|trim('/', 'right')
}}
{% endmacro %}
{# based on Postmark Transactional Email Templates
https://postmarkapp.com/transactional-email-templates #}
{# If you change button color, remember to pass it to macro AND define "buttonColor" before "extends" tag. #}
{# macro title #}
{% macro title(titleText) %}
{% if titleText is defined %}
<h1 style="margin-top: 0; color: #333333; font-size: 22px; font-weight: bold; text-align: center;" align="left">{{titleText}}</h1>
{% endif %}
@piotrpog
piotrpog / yt_address.twig
Last active July 6, 2021 10:35
Twig macro turning youtube video URL into responsive and lazy-loaded player. More info: http://craftsnippets.com/articles/responsive-and-lazy-loaded-youtube-videos-with-craft-cms
{# v2 #}
{%- macro ytAddress(url, lazy = true) -%}
{% if url is not empty %}
{% set id = url|split('v=')|last %}
{% set id = id|split('&')|first %}
{% set id = id|split('/')|last %}
<div class="youtube-player">
<figure style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin: 0px;">
<iframe src="https://www.youtube.com/embed/{{id}}" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"{{ lazy ? ' loading="lazy"'}}></iframe>