How to show a field only if it is not zero

Hello forum,

I need help:

I’m writing a text into a field tag_selection of tiddler $:/temp/Suche/Phrase
Than I’m filtering the tags according the tag_selection and let them count.

If tag_selection is empty, the count is 0.

The goal is to show the count only, if the tag_selection is not empty

<$let tag_selection={{$:/temp/Suche/Phrase!!tag_selection}}>
 (<$text text={{{ [!is[system]search::literal{$(searchTiddler)$}] :filter[enlist{!!tags}prefix<tag_selection>] +[count[]] }}}/>)

How can that be done?

Thanks for feedback.
Stefan

Hi @StS

Try adding +[!match[0]] at the end of your filter, it will mask the result when it’s 0.
If the only condition for the count to be 0 is tag_selection being empty, then you should be good to go.
If you don’t want to see the count when tag_selection is empty, then this simple solution won’t be enough.

Fred

Instead of using a simple $let widget to get the tag_selection field value, use a $list widget, like this:

<$list variable="tag_selection" filter="[[$:/temp/Suche/Phrase]get[tag_selection]]">

The get[tag_selection] filter operator will only return a result if the tag_selection field has non-blank content. When the field contents ARE blank, the filter result is empty, and entire the body of the $list widget is skipped.

enjoy,
-e

Hi @tw-FRed, @EricShulman, it was not successful.

Here an exampe to explain it better (= it’s an additional “subfilter”):
tag_selection is filled and counted → ok:

tag_selection is empty and only the count is shown.

I only want to see the count, if the tag_selection is not empty

Can that be archieved with an if/else in the code?

Thanks, Stefan

Ok, so instead of my previous solution, you can append :filter[<tag_selection>!is[blank]] to your filter.

It worked in this simplified example code:

<$edit-text field=tag_selection/>

<$let tag_selection={{!!tag_selection}}>
{{{ [enlist<tag_selection>] +[count[]] :filter[<tag_selection>!is[blank]] }}}
</$let>

Fred

Thanks, that works as expected :+1:

I just tested @EricShulman’s solution and it also works in my example.

<$edit-text field=tag_selection/>

<$list variable=tag_selection filter="[<currentTiddler>get[tag_selection]]">
<$text text={{{ [enlist<tag_selection>] +[count[]] }}}/>
</$list>

His solution is even better than mine because when tag_selection is empty, the <$text> widget and the filter are not rendered, so it uses less resources.
I’m curious to know why it doesn’t work in your code?

Fred

Has everyone forgotten the difference between has[fieldname] and has:field[fieldname] ?

The first is only true if field name has a value, the second only if the field exists empty or otherwise.

Also get[fieldname] only returns a value if there is one.

To test for a specific value else nothing use fieldname[value] or <value> etc… Possibly also !fieldname<value>

On all of the above set the current tiddler first.

Hi @tw-FRed,

replacng the $let witget with @EricShulman’s solution will not do the additional subfilter of tag_selection and the count is always 1

instead of using enlist<tag_selection>, try this:

<$list variable=tag_selection filter="[<currentTiddler>get[tag_selection]]">
<$text text={{{ [subfilter<tag_selection>count[]] }}}/>
</$list>

This will allow processing of any filter syntax contained within the tag_selection value. Alternatively, try using just the $count widget, like this:

<$count filter={{!!tag_selection}}/>

-e

Hi @EricShulman,

this is the working code:

\define phrase()
<span style="font-size: 0.7em; font-weight: 600; color: rgba(204, 204, 255, 0.6); border-bottom: 1px solid rgba(204, 204, 255, 0.6);">
      //... als Phrase://
</span>&nbsp;
<span style="font-size: 0.7em; color: rgb(144, 238, 144);"><$text text={{{ [!is[system]search::literal{$(searchTiddler)$}count[]] }}}/></span>
<span style="font-size: 0.5em; color: rgba(204, 204, 255, 0.6); margin-left: 2em;"> {{$:/temp/Suche/Phrase!!tag_selection}}</span>
<div class="show_tag_in_search"> ... Suche einschränken auf: &nbsp;<$edit-text tiddler="$:/temp/Suche/Phrase" field=tag_selection placeholder="Gruppe..." tag=input default=""/>
<$button class="tc-btn-invisible" set="$:/temp/Suche/Phrase!!tag_selection" setTo="" tooltip="Suchfeld löschen"> &nbsp; ↺ </$button>
</div> 
<$let tag_selection={{$:/temp/Suche/Phrase!!tag_selection}}>
<span style="font-size: 0.5em; color: rgb(144, 238, 144);"> <$text text={{{ [!is[system]search::literal{$(searchTiddler)$}] :filter[enlist{!!tags}prefix<tag_selection>] +[count[]] :filter[<tag_selection>!is[blank]] }}}/> </span>

<ul class="te-nav-list">
<!-- <$list filter="[!is[system]search:*:literal{$(searchTiddler)$}!sort[erstellt]limit[50]]" template="$:/plugins/telmiger/simple-search/ui/ListItemTemplate"/> -->
<$list filter="[!is[system]search:*:literal{$(searchTiddler)$}] :filter[enlist{!!tags}prefix<tag_selection>] +[!sort[erstellt]limit[50]]" template="$:/plugins/telmiger/simple-search/ui/ListItemTemplate">
</ul>

\end

Replacing the $let part with mentioned code didn’t work → filter with tag_selection has no effect anymore.
Is this the reason of using $:/temp/Suche/Phrase?