my variable just won't work with truncate function/filter

Hey guys, I’m quite new to Hubspot CMS development, so I’m not sure why my content variables are not working with the filter or function “truncate”.

Here is my dilemma which driving me mad:
The first and second line outputs are empty, while the 3rd and 4th show the correct output.

{% set mytitle = content.name %}

<li>{{ truncate(mytitle, 30, false, “…”) }} </li>

<li>{{ mytitle|truncate(30, false, “…”) }} </li>

<li>{{ truncate(“Whatever title here”, 10, false, “…”) }} </li> //dispays Whatever …

<li> {{ mytitle }} </li> //displays the page title correctly

Any help much appreciated, thanks.

Hi @andrasv I’m still learning to code a theme from scratch too so not 100% on this, based on themes I’ve worked with, I think you need something more like this:

{{ mytitle|truncate(content.name|default(30)) }}

Maybe @Anton or @alyssamwilie could confirm, they’re both much more experienced with coding than I am.

Hi @andrasv,

Am I assuming right, that you’re working with a custom module here?
If so the code should look like this:

{% set mytitle = module.name %} {# if your name is inside a group change it to module.GROUPNAME.name #}
...
{{ mytitle|truncate(30, false, '...') }}

If you’re working in a for-loop/repeater function the

{% set mytitle = module.name %}

has to be inside the loop.
Therefor the code should look something like this:

<ul>
{% for title in module.mytitles %}
{% set mytitle = title.name %}
<li>{{ mytitle|truncate(30, false, '...')</li>
{% endfor %}
</ul>

If you’re working with the blog post repeater

<ul>
{% for content in contents %}
<li>{{ content.name|truncate(30, false, '...') }}</li>
{% endfor %}
</ul>

If you’re working with something else, please describe or post the whole code.

Thank you!

best,

Anton

Thank you @Anton :fire::fire::fire:

Thank you, Anton, for your great help.
Yes, it is a custom module, which is called on a blogpost template. When I output the content.name, it rightly displays the blog title on the page. But when I try to use the truncate function on the content.name it doesn’t display anything.
The module.name is also not displaying anything, as it is not defined I guess.