Skip to content

Instantly share code, notes, and snippets.

@xavianaxw
Last active November 12, 2019 23:16
Show Gist options
  • Save xavianaxw/9078ca4fec92767cd8c3cf2493e510c7 to your computer and use it in GitHub Desktop.
Save xavianaxw/9078ca4fec92767cd8c3cf2493e510c7 to your computer and use it in GitHub Desktop.
Hubspot | getQueryParam() | Passing classes to menu item from Dashboard
{# Helpers: getQueryParam #}
{# Returns the query value by handle #}
{# Parameters #}
{# url | URL | Required #}
{# parameter | Parameter name | Required #}
{# How to import this macro to my module #}
{# {% from 'NOW_002_Website/helpers/get-query-param.html' import getQueryParam %} #}
{# How to use this macro #}
{# {{ getQueryParam('http://www.honestfox.com.au?class=custom', 'class') }} #}
{# How to check if value is returned by macro #}
{# if (getQueryParam('http://www.honestfox.com.au?class=custom', 'class') != '') {} #}
{%- macro getQueryParam(url, handle) -%}
{%- set urlParts = url | split('?') -%}
{%- if urlParts | length > 0 -%}
{%- set urlParams = urlParts | last | split('&') -%}
{%- if urlParams | length == 0 -%}
{%- set urlParams = [urlParams] -%}
{%- endif -%}
{%- for param in urlParams -%}
{%- if param is string_containing handle ~ '=' -%}
{{ param | split('=') | last }}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{% from [filepath] import getQueryParam %}
{# check if has '?class' query param to add special classes for styling #}
{% set childClass = getQueryParam(child.url, 'class') %}
<li class="{{ childClass }}">
{# regex is to remove '?class=' from url #}
<a href="{{ child.url | regex_replace('class=' ~ childClass, '') | regex_replace('[?]', '') }}"
target="{{ child.linkTarget }}"
>
{{ child.label }}
</a>
</li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment