# Templates Django TomSelect uses a flexible template system that allows for customization at multiple levels. All templates are located in the `django_tomselect/templates/django_tomselect/` directory. ## Template Structure ``` django_tomselect/templates/django_tomselect/ ├── helpers/ # Helper templates │ └── decode_if_needed.html # HTML entity decoding helper ├── render/ # Individual rendering components │ ├── clear_button.html # Clear selection button │ ├── dropdown_footer.html # Footer with actions │ ├── dropdown_header.html # Header with column titles │ ├── item.html # Selected item display │ ├── loading_more.html # Loading more results indicator │ ├── loading.html # Initial loading indicator │ ├── no_more_results.html # End of results message │ ├── no_results.html # No matches found message │ ├── not_loading.html # Non-loading state │ ├── optgroup_header.html # Option group header │ ├── optgroup.html # Option group container │ ├── option_create.html # Create new item option │ ├── option.html # Individual option display │ └── select.html # Base select element ├── tomselect_setup.html # Global setup and initialization ├── tomselect_token.html # Token search widget template └── tomselect.html # Main template ``` ## Main Template ### tomselect.html The main template that orchestrates the Tom Select initialization and rendering. ```django {% block select_element %} {% include "django_tomselect/render/select.html" with widget=widget %} {% endblock select_element %} {% block tomselect_init %} {# Tom Select initialization code #} {% endblock tomselect_init %} {% block tomselect_url_setup %} {# URL construction logic #} {% endblock tomselect_url_setup %} {% block tomselect_config %} {# TomSelect configuration object #} {% endblock tomselect_config %} {% block tomselect_plugins %} {# Plugin configuration #} {% endblock tomselect_plugins %} {% block tomselect_render %} {# Render functions for different components #} {% endblock tomselect_render %} {% block tomselect_extra_js %} {# Additional JavaScript #} {% endblock tomselect_extra_js %} ``` #### Customization Example ```django {# templates/myapp/custom_tomselect.html #} {% extends "django_tomselect/tomselect.html" %} {% block tomselect_init %} {{ block.super }} {# Add custom initialization code #} document.addEventListener("tomselect:initialized", function(e) { console.log("TomSelect initialized:", e.detail); }); {% endblock %} ``` ## Global Setup Template ### tomselect_setup.html This template is used to set up the global TomSelect configuration and initialization logic once per page, including an observer for dynamic content. ```django {% extends "django_tomselect/tomselect.html" %} {% load i18n %} {% load django_tomselect %} {% block tomselect_global_setup %} // Global namespace for django-tomselect if (!window.djangoTomSelect) { window.djangoTomSelect = { configs: new Map(), instances: new Map(), initialized: false, // ... other configuration // Setup MutationObserver for dynamic content setupObserver: function() { // ... observer setup code }, // Setup HTMX event handlers setupHtmxHandlers: function() { // ... htmx event handlers } }; } {% endblock tomselect_global_setup %} ``` The `