Skip to content

Instantly share code, notes, and snippets.

@reatlat
Forked from xavianaxw/slugify.html
Last active July 29, 2023 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reatlat/3632a8301a8a186382548c27a6572ea1 to your computer and use it in GitHub Desktop.
Save reatlat/3632a8301a8a186382548c27a6572ea1 to your computer and use it in GitHub Desktop.
Hubspot | slugify() | Converting text into slug for Hubspot
{# Helpers: slugify #}
{# Returns the slug version of a string #}
{# Parameters #}
{# string | String to convert into a slug | Required #}
{# How to import this macro to my module #}
{# {% from '[folder]/slugify.html' import slugify %} #}
{# How to use this macro #}
{# {{ slugify('Give me slug') }} #}
{%- macro slugify(text) -%}
{%- set output = text %}
{%- set output = output | string | lower | safe | striptags -%}
{%- set output = output | regex_replace("[\t\n\f\r ]", "-") -%} {# replace spaces with - #}
{%- set output = output | regex_replace("[^0-9A-Za-z_-]", "") -%} {# remove all non-word characters #}
{%- set output = output | regex_replace("-+", "-") -%} {# replace multiple - with single - #}
{%- set output = output | regex_replace("^[-]", "") -%} {# trim - from start of text #}
{%- set output = output | regex_replace("[-]$", "") -%} {# trim - from end of text #}
{{- output -}}
{%- endmacro -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment