unordered_list Filter

Data Structure

Documentation

Creates an unordered HTML list from a Python list that contains other lists, which also may contains lists, etc. You must add the outermost <ul> tags.

Variable

shopping = [
    'Food', [
        'Veggies', ['Tomatoes', 'Cucumbers', 'Peas'],
        'Fruit', ['Apples', 'Bananas'],
        'Meat', ['Chicken', 'Ham', 'Fish']
    ],
    'Other', ['Paper Towels', 'Napkins', 'Batteries']
]

Template

<ul>
  {{ shopping|unordered_list }}
</ul>

Result

<ul>
  	<li>Food
	<ul>
		<li>Veggies
		<ul>
			<li>Tomatoes</li>
			<li>Cucumbers</li>
			<li>Peas</li>
		</ul>
		</li>
		<li>Fruit
		<ul>
			<li>Apples</li>
			<li>Bananas</li>
		</ul>
		</li>
		<li>Meat
		<ul>
			<li>Chicken</li>
			<li>Ham</li>
			<li>Fish</li>
		</ul>
		</li>
	</ul>
	</li>
	<li>Other
	<ul>
		<li>Paper Towels</li>
		<li>Napkins</li>
		<li>Batteries</li>
	</ul>
	</li>
</ul>

Commentary

In theory, this is interesting, but it requires an oddly structured Python list to work correctly. It would make more sense if it worked with a structure like this that actually implies ownership:

shopping = {
    'Food': [
        {
            'Veggies': ['Tomatoes', 'Cucumbers', 'Peas']
        },
        {
            'Fruit': ['Apples', 'Bananas']
        },
        {
            'Meat': ['Chicken', 'Ham', 'Fish']
        }
    ],
    'Other': ['Paper Towels', 'Napkins', 'Batteries']
}

Did we get something wrong? Is there a use case for the unordered_list filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Oct. 30, 2022, 1:21 p.m. EST