Need help with if / else

Hello forum,

how can that be accomplished with if / else logic?

If listed tiddlers are not tagged → dont show this grafik

code:

<$edit-text field="search" placeholder="Search title"/>

<$let searchTerm={{!!search}}>
<$list filter="[<searchTerm>minlength[3]] :then[!is[system]search:title<searchTerm>]" template="$:/core/ui/ListItemTemplate_published_long"> <$link/> <br>
</$let>
</$list>

template $:/core/ui/ListItemTemplate_published_long:

<div class="tc-menu-list-item"><$link />
<span style="font-size: 0.75em; color: rgb(144, 238, 144); margin: 0 0.5em">
<$view field="published" format="date" template="DD. MMM YYYY"/>
</span>
<span style="font-size: 60%; vertical-align: middle;">
   <span style="color: rgb(179, 179, 179); margin: 0 0.5em"> aus Gruppe: </span>
   <span style=" opacity: 0.9;"> <$macrocall $name=tag-pill tag={{!!tags}}/> </span>
</span>
</div>

Result:

Thanks, Stefan

2 Likes

@StS without delving into your code example to understand exactly what your challenge is perhaps this will help.?

  • You could explain the issue a little more, to make it easier to answer.

The normal If / Else structure is somewhat simplistic when compared with the logic available within filters,

  • Often the then or else is handled if “any result is generated”, and in some cases the else is emptyMessage.
  • Similarly if you have only one then or else action, and you can switch the filter so nothing happens in the reverse case.

However when we generate a result in a filter the “not cases” ! are really easy to pile on to limit the filter to very special cases; for example;

[all[tiddlers]object-type[myobject]!tag[done]!has[done-date]!has[archived-date] ....
  • Here you can see it is easy to make a filter increasingly specific through the addition of as many Not qualifications to a very deep level.
  • If we can phrase a filter this way and use the content or the emptyMessage to respond to the then/else cases we are using an if/then/else.
  • now with 5.3.x we can move a large set of not filters into a function, or custom operator.
\function is.active() [!tag[done]!has[done-date]!has[archived-date]]

filter="[all[tiddlers]is.active[]]" 

OR

filter="[all[current]is.active[]]" 

Another approach is to have two list or reveal widgets, with two different filters in use, one produces a result for the then case, the second the exact opposite for the else case.

Hi @TW_Tones,

what I’m doing with the code?

  • I will output a list of tiddlers (in this case: search for tiddlers according the input) → works fine
  • for the output I use a template (= $:/core/ui/ListItemTemplate_published_long) showing the title, a “private” date-field (=published) and the tags of that tiddler → works fine

If a tiddler has no “private” date-field (=published) and no tag, the output looks like

grafik

My question is:
How do I display only the title (remove grafik in the output list) if the tiddler has no tag?

Thanks, Stefan

we don’t have $ifbut we have $list; the strategy is “show that part only if the tiddler has at least a tag”, or in tiddlywiki-filter-ese, unless is untagged.

In your case, you should put the second, top-level span inside a second $list, like this:

<$list filter="[<currentTiddler>!untagged[]]">

2 Likes

Thanks @jerojasro, thats it - works nice. :+1:

Totally a solution answer, as @StS flags it. :white_check_mark:

For the sake of novices arriving at this thread wanting to learn how to conditionally display something within a tiddler, it may be useful to encourage the habit of appending limit[1] before closing the filter and appending variable="" or variable=null to the list widget.

In this particular case it makes no difference, because this filter returns exactly the current tiddler (or none at all). But often we want some element to display only when (for example) some other tiddler[s] tag this one. And then we’ll need a bit more caution:

<$list filter="[<currentTiddler>tagging[]limit[1]]" variable=null>

{{{ [<currentTiddler>tagging[]count[]] }}} tiddlers tagging this one

</$list>

Without limiting the filter to one result, and specifying the null variable for the list, the list would evaluate separately for each tiddler tagging this one, and never include the intended content for the current tiddler…

This caveat is for folks who share OP’s same intuitive goal — “All I want to do is conditionally display this part” — but who would be misled by trying to adapt the concise solution offered by @jerojasro. :slightly_smiling_face:

(Yesterday, after offering a 3-minute whirlwind background beyond certain world events to my curious teen, the kid said with a wink, “Wow, are you a teacher or something?” Yes. And whether for better or for worse, I tend to linger over nudging a forum like this so it serves as a teaching resource beyond addressing the OP’s case.)

4 Likes

According to the docs, there is a special if-then-else syntax for this purpose since version 5.3.2.

Indeed, and welcome to the forum, @krvkir!

Although the <% if %> syntax is technically a “shortcut” (since it was possible to achieve the same results before), it makes it much easier to handle this common kind of need.

Using this conditional shortcut syntax also makes it less confusing for others (and one’s own future self, perhaps) to read and troubleshoot this kind of technique. The “if/else” pattern of thought can feel so different, intuitively, from the <$list> technique that we needed to employ before.

Still, there are many plugins and example solutions out there that predate the conditional shortcut syntax, so it’s helpful to become familiar with how the list widget can display things conditionally (and is still doing that work, behind the scenes).

2 Likes