Last active
July 29, 2023 18:50
-
-
Save xavianaxw/b683fa0967d36faf619530eb3ed59c1f to your computer and use it in GitHub Desktop.
Hubspot | slugify() | Converting text into slug for Hubspot
This file contains 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 -%} | |
{%- 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 -%} |
@xavianaxw sometimes you may use slugify macros for custom text fields, where content managers may pass html tags like <br/>
or do something with span wrappers, and to avoid issues, I think will be nice to have safe
and striptags
filters
something like this:
{%- set output = output | string | lower | safe | striptags -%}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remember to remove linebreaks when setting up on Hubspot. Else it will cause a linebreak when including them into html.