CMS Development

BillOddie
Miembro

Dynamically populate choices in custom module choice field

resolver

I am trying to dynamically populate the choices in a choice field. 

 

I am building a list from a hubDB, and then joining the list to make a comma seperated string of choices ready to pass into the choice module declaration.

 

The code is in the Additional <head> markup section of my blog template.

 

Sadly I Get and error saying...

 

'Error:Unexpected token [EndTag] when in state [InBody]'

 

And it wont let me publish the page. Is there any way i can get round this?

 

{% set currentList = [] %}
{% for row in hubdb_table_rows(1034058, "orderBy=random()") %}
  {{ currentList.append(row.name|striptags) }}
{% endfor %}

{% set currentString = currentList|join(',') %}

{% choice "test" label='Test', value='test0', choices='{{ currentString }}' export_to_template_context=true %}

0 Me gusta
1 Soluciones aceptada
BillOddie
Solución
Miembro

Dynamically populate choices in custom module choice field

resolver

I've managaed to solve this my self. I had to change the line of code within the for loop in order to get it working. I changed it to this...

 

{% set items = currentList.append(row.name|striptags) %}

 

...the main differences being I am now using the hubl tag '{% %}' instead of '{{ }}', and I have had to set the return value to a variable. I never use this varable, but with out it the design tools throw an error and you can't publish the page.

 

I'm guessing that using the '{{ }}' was trying to out put something to the page, which was inside the <head> tag, so then it was erroring.

 

Hope this helps someone else.

 

 

Ver la solución en mensaje original publicado

1 Respuesta 1
BillOddie
Solución
Miembro

Dynamically populate choices in custom module choice field

resolver

I've managaed to solve this my self. I had to change the line of code within the for loop in order to get it working. I changed it to this...

 

{% set items = currentList.append(row.name|striptags) %}

 

...the main differences being I am now using the hubl tag '{% %}' instead of '{{ }}', and I have had to set the return value to a variable. I never use this varable, but with out it the design tools throw an error and you can't publish the page.

 

I'm guessing that using the '{{ }}' was trying to out put something to the page, which was inside the <head> tag, so then it was erroring.

 

Hope this helps someone else.