{% if data.text %}
  {% set default_option = data.text %}
{% elif 'default' in schema %}
  {% set default_option = schema.default %}
{% else %}
  {% set default_option = '' %}
{% endif %}
{% if default_option %}
  {% set ns = namespace(default_option='') %}
  {% for option in schema.choices %}
    {% if option == default_option %}
      {% set ns.default_option = option %}
    {% endif %}
  {% endfor %}
  {% set default_option = ns.default_option %}
{% endif %}

<select {% if is_template %} class="template-select" data-template-class="form-control selectpicker choicepicker" {% else %} class="choicepicker form-control selectpicker" {% endif %} data-sampledb-default="{{ default_option }}" {% if schema.choices | length >= config['MIN_NUM_TEXT_CHOICES_FOR_SEARCH'] >= 0 %}data-live-search="true"{% endif %} name="{{ id_prefix }}_text">
  {% if form_data.get(id_prefix+'_text', '') != '' or (data is not none and data.get('text', '') != '') %}
    {% if not is_required %}
      <option value="">&mdash;</option>
    {% endif %}
  {% else %}
    {% if not (is_template and schema['default'] and is_required) %}
      <option selected="selected" value="">&mdash;</option>
    {% endif %}
  {% endif %}
  {% for option in schema.choices %}
    {% if id_prefix+'_text' in form_data %}
      {% set is_selected = form_data[id_prefix+'_text'] == ( option | to_string_if_dict ) %}
    {% elif data is not none and "text" in data %}
      {% set is_selected = data.text == option %}
    {% else %}
      {% set is_selected = false %}
    {% endif %}
    <option value="{{ option }}" data-value-base64="{{ option | base64encode }}" {% if is_selected %}selected="selected"{% endif %}>{{ option | get_translated_text }}</option>
  {% endfor %}
</select>
