Convert 'widget_data' text value to a hyphenated text value with hubL

SOLVE
Occasional Contributor

I'm looking to take a value that could be a string of words (i.e. 'A Partner Name') and first, convert it to lower case and then to join the words with hyphens.

 

Is this possible with hubL? As far as I can tell, the only supported method is to lowercase, but not to string them.

 

Desired output example:

 

 

{% text "partner_name" label="Partner Name", export_to_template_context=True %}
<img class="{{ widget_data.partner_name.value }}">

partner_name = A Partner Name
<img class = "a-partner-name">

 

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
Regular Advisor

{% text "partner_name" label="Partner Name", export_to_template_context=True %}
<img class="{{ widget_data.partner_name.value | lower | replace(' ', '-') }}">

5 Replies 5
Regular Advisor

{% text "partner_name" label="Partner Name", export_to_template_context=True %}
<img class="{{ widget_data.partner_name.value | lower | replace(' ', '-') }}">

Occasional Contributor

Many thanks, that works beautifully. 

 

How lovely it would be to have the ability to more simply slugify.

Advisor

Ideally, HubSpot would have a "slugify" HubL filter. However, adding to @ndwilliams3 reply, you can also combine multiple "replace" filters on top of each other to mimic the slugify behavior. E.g.:

<img class="{{ widget_data.partner_name.value|replace(' ','-')|replace(',','')|replace('*','') }}">

It feels a little dirty but it gets the job done. 

--
Stefen Phelps
Web Developer / Co-founder
Kelp Creative Agency
Twitter — @stefen
Highlighted
Regular Advisor

@stefen I agree a slugify filter would be great!

Regular Contributor

If anyone is looking for a way to slugify text, but needs have a function thats closer to URL safe for Rich Text data, here's one I've used: 

item.rich_text_field|striptags|lower|trim|truncate(40)|urlencode|replace('%', '')|replace('+','-')|replace('*', '')|replace('|', '')|replace('.', '')|replace(',', '')|replace('!', '')

It's battle tested with real client content managers in production.

Reply
0 Upvotes