# Evaluate non-empty list to true in mustache

**URL:** <https://discuss.elastic.co/t/evaluate-non-empty-list-to-true-in-mustache/216864>\
**Category:** Elasticsearch\
**Created:** [January 28, 2020, 2:44pm UTC](https://discuss.elastic.co/t/evaluate-non-empty-list-to-true-in-mustache/216864 "2020-01-28T14:44:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![styks90](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/styks90/32/59205_2.png) [@styks90](https://discuss.elastic.co/u/styks90)\
**Post date:** [January 28, 2020, 2:44pm UTC](https://discuss.elastic.co/t/evaluate-non-empty-list-to-true-in-mustache/216864/1 "2020-01-28T14:44:27Z")

</div>

Quick question on mustache:

Is it possible to evaluate a conditional on a list just once? That is, instead of iterating and applying the template to each element in the `array`, evaluate it as non-empty and do stuff in it only once. For example

```
{{#array}}
{"terms": { "foo": "{{#toJson}}bar{{/toJson}}" }}
{{/array}}

```

would be rendered as

```
{"terms": {"foo": ["bar1", "bar2", "bar3"]}}

```

if array is a non-empty list, and if empty the block is not evaluated. Tags with # and / are placeholders. Hope this makes sense.

Note: If this is syntactically not possible with mustache, we can obviously just surround all of the sub-queries with something like a bool-should block. This however necessitates some akward stuff with commas or a final `match_all`, `match_none` query.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 29, 2020, 3:25pm UTC](https://discuss.elastic.co/t/evaluate-non-empty-list-to-true-in-mustache/216864/2 "2020-01-29T15:25:48Z")

</div>

Just in case you have not seen it, there is a quick paragraph about conditionals in mustache in the search template docs [https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-template.html#search-template-conditional-clauses](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-template.html#search-template-conditional-clauses)

I am however not absolutely sure, if that solves your use-case in this problem.

---

<div class="post-metadata">

**Author:** ![styks90](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/styks90/32/59205_2.png) [@styks90](https://discuss.elastic.co/u/styks90)\
**Post date:** [January 29, 2020, 3:43pm UTC](https://discuss.elastic.co/t/evaluate-non-empty-list-to-true-in-mustache/216864/3 "2020-01-29T15:43:34Z")

</div>

Unfortunately this does not seem to be a use case which is covered by mustache ... anyway, we were able to restructure the query in a way that uses the elements of the array (which is probably what you'd rather do.)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 26, 2020, 3:43pm UTC](https://discuss.elastic.co/t/evaluate-non-empty-list-to-true-in-mustache/216864/4 "2020-02-26T15:43:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
