-
-
Save reatlat/3632a8301a8a186382548c27a6572ea1 to your computer and use it in GitHub Desktop.
Hubspot | slugify() | Converting text into slug for Hubspot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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