Skip to content

Instantly share code, notes, and snippets.

@davidhellmann
Created July 24, 2018 12:08
Show Gist options
  • Save davidhellmann/407c7b0cec475d010d1aa3ddc046776d to your computer and use it in GitHub Desktop.
Save davidhellmann/407c7b0cec475d010d1aa3ddc046776d to your computer and use it in GitHub Desktop.
Craft CMS 3 Nav with Macros
{
"fieldGroups": [
"Globals"
],
"fields": [
{
"group": "Globals",
"name": "Nav Main",
"handle": "globalNavMain",
"instructions": "",
"type": "craft\\fields\\Matrix",
"minBlocks": "",
"maxBlocks": "",
"localizeBlocks": "1",
"blockTypes": [
{
"name": "Menu Item",
"handle": "menuItem",
"fields": [
{
"required": false,
"translationMethod": "site",
"name": "Menu Item",
"handle": "menuItem",
"instructions": "",
"type": "typedlinkfield\\fields\\LinkField",
"typesettings": {
"allowCustomText": "1",
"allowedLinkNames": [
"url",
"category",
"entry"
],
"allowTarget": "1",
"defaultLinkName": "url",
"defaultText": "",
"typeSettings": {
"url": {
"disableValidation": "1"
},
"email": {
"disableValidation": ""
},
"tel": {
"disableValidation": ""
},
"asset": {
"sources": "*"
},
"category": {
"sources": "*"
},
"entry": {
"sources": "*"
}
}
}
},
{
"required": false,
"name": "Visible",
"handle": "visible",
"instructions": "",
"type": "craft\\fields\\Lightswitch",
"typesettings": {
"default": ""
}
},
{
"required": false,
"translationMethod": "site",
"name": "Highlight Triggers",
"handle": "highlightTriggers",
"instructions": "Please do not edit this field. It's needed for development stuff. ",
"type": "craft\\fields\\PlainText",
"typesettings": {
"placeholder": "\u2014",
"code": "",
"multiline": "",
"initialRows": "4",
"charLimit": "",
"columnType": "text"
}
},
{
"required": false,
"name": "Segment Position",
"handle": "segmentPosition",
"instructions": "",
"type": "craft\\fields\\Number",
"typesettings": {
"decimals": 0
}
},
{
"required": false,
"name": "Icon Name",
"handle": "iconName",
"instructions": "",
"type": "craft\\fields\\PlainText",
"typesettings": {
"placeholder": "",
"code": "",
"multiline": "",
"initialRows": "4",
"charLimit": "",
"columnType": "text"
}
}
]
}
]
}
],
"globalSets": [
{
"name": "Nav Main",
"handle": "navMain",
"fieldLayout": {
"Content": [
"globalNavMain"
]
}
}
]
}
{# @var craft \craft\web\twig\variables\CraftVariable #}
{# @var entry \craft\elements\Entry #}
{#
Set Active Class
------------------------------------------------------------
{% import '_partials/macros/_macro-isActive.html' as macroIsActive %}
{{ macroIsActive.isActive('news', -1, 'cn') }}
#}
{% macro isActive(segments, position, prefix, cn) %}
{% spaceless %}
{% set segments = segments | default(null) %}
{% set position = position | default(-1) %}
{% set prefix = prefix | default(null) %}
{% set cn = cn | default('is-active') %}
{% if segments is iterable and craft.app.request.getSegment(position) in segments %}
{{ prefix ? prefix ~ cn : cn }}
{% elseif craft.app.request.getSegment(position) == segments %}
{{ prefix ? prefix ~ cn : cn }}
{% endif %}
{% endspaceless %}
{% endmacro %}
{# @var craft \craft\web\twig\variables\CraftVariable #}
{# @var entry \craft\elements\Entry #}
{#
Description of what this file is for
@package neubau_com.neubau-eyewear.www
@author David Hellmann — Fredmansky [[email protected]]
m-navMain
------------------------------------------------------------
#}
{# -- Set Defaults -- #}
{% set defaults = {
cn: 'm-navMain',
modifiers: [],
data: {},
js: null,
waypoint: null,
waypointAni: null,
items: null,
cart: null
} %}
{# -- Merge Default with Options -- #}
{% set opt = opt is defined ? defaults|merge(opt) : defaults %}
{# -- Macros -- #}
{% import '_partials/macros/_macro-isActive.html' as macroIsActive %}
{# -- Modul -- #}
{% if opt.items %}
<nav class="{{ opt.cn }}
{% for modifier in opt.modifiers %}
{{ modifier | length ? ' ' ~ opt.cn ~ '--' ~ modifier }}
{% endfor %} {{ opt.js ? opt.cn|replace({ 'm-' : 'js-' }) : '' }}
{{ opt.waypointAni ? opt.waypointAni : '' }}"
{{ opt.waypoint ? ' data-waypoint' : '' }}
{% for key, value in opt.data %}
data-{{ key }}="{{ value }}"
{% endfor %}>
<ul class="{{ opt.cn ~ '__list' }}">
{% for item in opt.items if item.visible %}
<li class="{{ opt.cn ~ '__listItem' }}">
{# -- Get Position -- #}
{% set segmentPosition = item.segmentPosition + 0 %}
{# -- Get URL Segment from Position -- #}
{% set urlSegment = craft.app.request.getSegment(segmentPosition) %}
{# -- Get Position -- #}
{% set segments = item.highlightTriggers | default('notSet') | replace({'home': ''}) | split(',') %}
{# -- Set Text -- #}
{% set text = item.menuItem.customText != '' ? item.menuItem.customText : item.menuItem.text | default(item.menuItem.url) %}
{# -- link -- #}
{% include '_atoms/link/_template.html' with {
opt: {
modifiers: [
'header'
],
customClasses: [
macroIsActive.isActive(segments, segmentPosition) | trim
],
text: text,
targetBlank: item.menuItem.target != '' ? true : false,
url: item.menuItem.url
}
} only %}
</li>
{% endfor %}
</ul>
</nav>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment