# Dynamically populate choices in custom module choice field

**URL:** <https://community.hubspot.com/t/dynamically-populate-choices-in-custom-module-choice-field/12917>\
**Category:** CMS Development\
**Tags:** hubdb\
**Created:** [February 11, 2019, 10:12am UTC](https://community.hubspot.com/t/dynamically-populate-choices-in-custom-module-choice-field/12917 "2019-02-11T10:12:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BillOddie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/billoddie/32/31184_2.png) [@BillOddie](https://community.hubspot.com/u/BillOddie)\
**Post date:** [February 11, 2019, 10:12am UTC](https://community.hubspot.com/t/dynamically-populate-choices-in-custom-module-choice-field/12917/1 "2019-02-11T10:12:09Z")

</div>

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 %}

---

<div class="post-metadata">

**Author:** ![BillOddie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/billoddie/32/31184_2.png) [@BillOddie](https://community.hubspot.com/u/BillOddie)\
**Post date:** [February 11, 2019, 10:34am UTC](https://community.hubspot.com/t/dynamically-populate-choices-in-custom-module-choice-field/12917/2 "2019-02-11T10:34:23Z")

</div>

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.
