CMS Development

BillOddie
Member

How do I concatenate strings with HUBL?

How can I concatenate strings with Hubl? The docs suggest there is a special operator for it (~), however I have tried to use it in many different ways and I can't preduce the behaviour I expect.

 

I need to loop through an list and build up a string with the contents of the list. I have tried this and after the loop the 'items' variable has no value...

 

{% set items = "" %}

{% for row in hubdb_table_rows(1034058, "orderBy=random()") %}
{{ items = items ~ ("Test"+loop.index0+", ") }}
{% endfor %}
<div>{{ items }}</div>

 

Has anyone managed to do this before?

6 Replies 6
webdew
Guide | Diamond Partner
Guide | Diamond Partner

How do I concatenate strings with HUBL?

Hi @BillOddie ,

Please try the below code:

{% set str_C = '' %}
<ul>
    <li data-position="{{ row.price }}" data-ID="{{ c }}" class="content domain-block project f-{{ row.sort_by[0].id }} {% for new_row in row.sort_by %}{% if(str_C != '') %}{% set str_C = str_C ~ ' ' ~ new_row.name|replace(' ','-') %}{% else %}{% set str_C = ' ' ~ new_row.name|replace(' ','-') %}{% endif %}{{str_C|replace('_',' ')}}{% endfor %}" data-filter="f-{{ row.sort_by[0].id }}">
    </li>
</ul>ORPlease try this but not passed values out the for loop:
{% set test_varC = '' %}
{% macro render_dialog(items,test_varC) %}  {% for x in items %}
    {% set test_varC = test_varC ~ x %}
    <h1>
    {{test_varC}}
</h1>
  {% endfor %}{% endmacro %}
{% set items = ['item 1', 'itme 2', 'item 3'] %}// Function call
 {% call render_dialog(items,test_varC) %}
 {% endcall %}

Hope this helps!

If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards. 

0 Upvotes
stefen
Key Advisor | Partner
Key Advisor | Partner

How do I concatenate strings with HUBL?

@BillOddie you'll want to use the tilde in the statement rather than the expression. For example:

{% set items = "string 1" ~ "string 2" %}

Hope that helps!

Stefen Phelps, Community Champion, Kelp Web Developer
JVidal7
Participant | Platinum Partner
Participant | Platinum Partner

How do I concatenate strings with HUBL?

Bless you Stefen! Where did you get this info? I didn't find it in the HubL doc.

BillOddie
Member

How do I concatenate strings with HUBL?

Hello, 

 

Thank you both for your answers.

 

I ended up using join| filter to print the list as a string with a custom delimiter. 

 

Sorry if my question was confusing

 

piersg
Key Advisor

How do I concatenate strings with HUBL?

@BillOddie 

I think a problem here may have been trying to access a variable that's assigned a value in a for loop outside of said loop

 

So instead of 

{% set items = "" %}
{% for row in hubdb_table_rows(1034058, "orderBy=random()") %}
  {{ items = items ~ ("Test"+loop.index0+", ") }}
{% endfor %}
<div>{{ items }}</div>

Where {{items}} has no value because you're defining it and displaying it outside of the for loop which would assign it value.

 

I think you could do

 

{% set items = "" %}
{% for row in hubdb_table_rows(1034058, "orderBy=random()") %}
  {{ items = items ~ ("Test"+loop.index0+", ") }}
  <div>{{ items }}</div>
{% endfor %}  

 

0 Upvotes
Jon_McLaren
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

How do I concatenate strings with HUBL?

 

{% set items = [] %}
{% for x in array %}
       {% set _dummyvar = items.append(x) %}
{%endfor %}

{{items}}


This wont get you 100% of the way but will get you most of the way. Wasn't entirely sure I understood what you were intending to do.

Messages posted by this account have been preserved for their historical usefulness. Jon has a new profile now.