Function Issue with 4 Else Conditions

Am I encountering a bug? The below function will not execute the 4th condition. It always keeps displaying the 3rd when the field task-priority is set to critical.

\function test-bg()
  [get:field[task-priority]match[low]then[skyblue]]
	:else[get:field[task-priority]match[medium]then[purple]]
	:else[get:field[task-priority]match[high]then[red]]
	:else[get:field[task-priority]match[critical]then[gray]]
\end test-bg

Color:<<test-bg>>

I believe the function does not like the word high. When I change the match to from high to hoop then the value critical works. I do not think the filter that I have is working reliably.

You don’t need to use “:field” with the get operator, and you don’t need to repeat the name of the function at the end if it is not nested, but otherwise it seems to work correctly:

wikitext:

\function test-bg()
 	[get[task-priority]match[low]then[skyblue]]
	:else[get[task-priority]match[medium]then[purple]]
	:else[get[task-priority]match[high]then[red]]
	:else[get[task-priority]match[critical]then[gray]]
\end

task-priority: <$select field="task-priority">
<$list filter="low medium high critical"><option>{{!!title}}</option></$list>
</$select>

test-bg: <<test-bg>>

online demo

Thank you for your help on this. I believe there is still an issue. I think I figured it out though. The filter needs to specify the current tiddler. In your demo, if you add another tiddler with a field called task-priority and set a value. The current function is picking up the task-priority from other tiddlers.

This seems to work so far by adding is[current]

\function test-bg()
 	[is[current]get[task-priority]match[low]then[skyblue]]
	:else[is[current]get[task-priority]match[medium]then[purple]]
	:else[is[current]get[task-priority]match[high]then[red]]
	:else[is[current]get[task-priority]match[critical]then[gray]]
\end

edit: had to add is[current] to every else condition

As you’ve already realized, when [get[...]] occurs as the first operator in a filter run, it gets ALL tiddlers that have that field value. Since each filter run is processed separately, you should precede each get[...]] in the same way.

  • Use all[current] (or even just <currentTiddler>) rather than is[current]. The former is a selection constructor operator, which ignores any preceding filter input items, while the latter is a “selection modifier” that generally wants some preceding input (but falls back to functioning like a selection constructor if it is the first operator in a filter run).
  • Note that <currentTiddler> is slightly more efficient that either all[..] or is[...] since it just retrieves the value of the current tiddler variable and doesn’t need to parse the “all” or “is” operator name.

For consistent results, I recommend writing your function like this:

\function test-bg()
 	[<currentTiddler>get[task-priority]match[low]then[skyblue]]
	:else[<currentTiddler>get[task-priority]match[medium]then[purple]]
	:else[<currentTiddler>get[task-priority]match[high]then[red]]
	:else[<currentTiddler>get[task-priority]match[critical]then[gray]]
\end

enjoy
-e

I might simplify this a little by adding a helper function:

\function priority() [<currentTiddler>get[task-priority]]
\function test-bg()
 	[<priority>match[low]then[skyblue]]
	~[<priority>match[medium]then[purple]]
	~[<priority>match[high]then[red]]
	~[<priority>match[critical]then[gray]]
\end