CMS Development

dh-agent
Participante

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

resolver

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 Me gusta
1 Soluciones aceptada
ndwilliams3
Solución
Asesor destacado

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

resolver

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

Ver la solución en mensaje original publicado

5 Respuestas 5
keegan-sbm
Colaborador

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

resolver

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 Me gusta
stefen
Asesor destacado | Partner
Asesor destacado | Partner

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

resolver

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
Asesor destacado

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

resolver

@stefen I agree a slugify filter would be great!

ndwilliams3
Solución
Asesor destacado

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

resolver

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

dh-agent
Participante

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

resolver

Many thanks, that works beautifully. 

 

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