Home > database >  Check for a list the elements of which all contain only whitespaces in HTML
Check for a list the elements of which all contain only whitespaces in HTML

Time:07-07

I'm building an app in Flask. I'm extracting a variable items (which is a list) and displaying each of its elements in a separate cell.

<tr>
   <th>Some heading</th>
   {% for item in items %}
      <td>{{ item }}</td>
   {% endfor %}
</tr>

However, some items lists only contain elements consisting of whitespaces only, for example:

items = [' ', ' ', ' ', ' ']

I want to check that this is NOT the case and create a table row only if at least one element of items contains more than whitespaces. {% if items %} doesn't help because the list has elements. Is there any other way? I don't know JavaScript, but I would be glad to look into that too if there is a way.

CodePudding user response:

With python you can check it with:

if all(map(lambds s: s.strip(),items)):

With JS

if (items.every((item) => item.trim()))

CodePudding user response:

Sorry, found a way out through introducing an items_check in the app file. Basically, I joined the elements into a string and checked it for containing only whitespace. Now {% if items_check %} works :)

  • Related