CMS Development

dh-agent
Participant

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

SOLVE

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">

 

0 Upvotes
1 Accepted solution
ndwilliams3
Solution
Key Advisor

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

SOLVE

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

View solution in original post

5 Replies 5
keegan-sbm
Contributor

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

SOLVE

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.

0 Upvotes
stefen
Key Advisor | Partner
Key Advisor | Partner

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

SOLVE

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, Community Champion, Kelp Web Developer
ndwilliams3
Key Advisor

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

SOLVE

@stefen I agree a slugify filter would be great!

ndwilliams3
Solution
Key Advisor

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

SOLVE

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

dh-agent
Participant

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

SOLVE

Many thanks, that works beautifully. 

 

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