Skip to content

Instantly share code, notes, and snippets.

@jnowland
Last active February 28, 2017 16:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jnowland/99b5fa5d4dba564fc64e to your computer and use it in GitHub Desktop.
Save jnowland/99b5fa5d4dba564fc64e to your computer and use it in GitHub Desktop.
Craft Snippets
{# Shows all categories in the news group #}
{% set categories = craft.categories.group('news') %}
<ul>
{% nav category in categories %}
<li>
<a title="{{ category.title }}" href="{{ category.url }}">{{ category.title }}</a>
{# Output a nested <ul> if this category has any children #}
{% ifchildren %}
<ul>{% children %}</ul>
{% endifchildren %}
</li>
{% endnav %}
</ul>
{# Spits out all entries with the category 'events' in 'news' channel #}
{% set eventCategory = craft.categories.find({group: 'news', slug: 'events', limit: 1}) %}
{% set eventEntries = craft.entries.section('news').relatedTo(eventCategory).limit(5).find() %}
{% for entry in eventEntries %}
{{ entry.title }}
{% endfor %}
{# If you need to use handle bar tempaltes that use the same template as twig you can use the raw tags to get around this! #}
<figure class="Gallery js-Gallery"
data-cycle-fx=scrollHorz
data-cycle-prev="#prev"
data-cycle-next="#next"
data-cycle-caption=".js-GalleryCount"
{% raw %}data-cycle-caption-template="{{slideNum}} of {{slideCount}}"{% endraw %}
></div>
{% if 'events' in entry.newsCategories | group('slug') | keys %}
{# Has events as a category assigned to it. #}
{% endif %}
{% nav item in craft.entries.section('navigation').find({level: '>1', descendantOf: headerNav}) %}
{# Logic to set the active class and url #}
{% if item.type == 'nvNavigationEntry' and item.nvTargetEntry|length > 0 %}
{% set navHref = url(item.nvTargetEntry[0].uri) %}
{% set navIsActive = (craft.request.url == url(item.nvTargetEntry[0].uri) ? true : false) %}
{% else %}
{% set navHref = item.nvRemoteUrl %}
{% set navIsActive = (craft.request.url == url(item.nvRemoteUrl) ? true : false) %}
{% endif %}
<li class="Nav-item{% if navIsActive %} is-active{% endif %}">
<a href="{{ navHref }}">
{{ item.title }}
</a>
{% ifchildren %}
<ul class="PrimaryNav-secondTier">
{% children %}
</ul>
{% endifchildren %}
</li>
{% endnav %}
{# Stripes all tags but <strong> and <em> #}
{{ entry.intro | striptags('<strong><em>')|raw }}
{# Set up the thumb manipulations #}
{%
set thumbnail = {
mode: 'crop',
width: 63,
height: 63,
quality: 85,
position: 'center-center'
}
%}
{# Set the Source #}
{% set imagePlaceholderVar = entry.ImageAssetField[0] %}
<img src="{{ imagePlaceholderVar.getUrl(thumbnail) }}" width="{{ imagePlaceholderVar.getWidth(thumbnail) }}" height="{{ imagePlaceholderVar.getHeight(thumbnail) }}" class="u-mediaBlockPreview Thumbnail--rounded margin--right8" />
{# Paged Pagination that correctly shows the first and last page number only if they are not already in the pagination #}
<ul class="Pagination">
{% if paginate.prevUrl %}
<li class="Pagination-item Pagination-prev">
<a href="{{ paginate.prevUrl }}">
<span class="icon ChevronLeftGreen">&lt;</span> Prev
</a>
</li>
{% endif %}
{% for page, url in paginate.getPrevUrls(1) %}
{# Always show the "first page" url if the pagiation offset is out of range #}
{% if loop.first and paginate.firstUrl != url %}
<li class="Pagination-item">
<a href="{{ paginate.firstUrl }}" class="Pagination-number">1</a>
<span class="Pagination-hellips">&hellip;</span>
</li>
{% endif %}
<li class="Pagination-item">
<a href="{{ url }}" class="Pagination-number">{{ page }}</a>
</li>
{% endfor %}
{# Always output the current page #}
<li class="Pagination-item is-current">
<span class="Pagination-number">{{ paginate.currentPage }}</span>
</li>
{% for page, url in paginate.getNextUrls(1) %}
<li class="Pagination-item">
<a href="{{ url }}" class="Pagination-number">{{ page }}</a>
</li>
{# Don't show the "total page" url if it's in the pagiation already #}
{% if loop.last and paginate.lastUrl != url %}
<li class="Pagination-item">
<span class="Pagination-hellips">&hellip;</span>
<a href="{{ paginate.lastUrl }}" class="Pagination-number">{{paginate.totalPages}}</a>
</li>
{% endif %}
{% endfor %}
{% if paginate.nextUrl %}
<li class="Pagination-item Pagination-next">
<a href="{{ paginate.nextUrl }}">
Next <span class="icon ChevronRightGreen">&gt;</span>
</a>
</li>
{% endif %}
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment