HubL text transformation - converting strings to URL-friendly format in HubSpot templates

I’m working on a HubSpot template and need help with transforming text strings into URL-friendly formats using HubL.

Basically, I want to take regular text like Sample Content and turn it into sample-content (lowercase with hyphens instead of spaces). I need this functionality to create dynamic CSS class names based on user input from module fields.

I’m familiar with WordPress where you can use sanitize_title() for this purpose, but I can’t find a similar built-in filter in HubL. Since HubL runs on Python, I’m wondering if there are any Python string methods that work within HubL templates.

Has anyone managed to implement this kind of text slugification in HubSpot? Any suggestions or code examples would be really helpful.

You can do this with HubL’s string filters - just chain replace with lower. Here’s what works:

{{ your_text_variable|lower|replace(" ", "-")|replace("_", "-") }}

This converts “Sample Content” to “sample-content”. For special characters, chain more replace filters to handle punctuation.

I’ve used this in custom modules where I needed dynamic CSS classes from content titles. One downside: HubL doesn’t have Python’s slugify method, so you’re stuck with basic filters.

Before going live, test it with numbers, punctuation, and special characters to make sure your filter chain covers all the weird edge cases.