Only show custom view template tiddler under certain condition?

Hi all

I have a tiddler tagged $:/tags/ViewTemplate. But I would like it only to appear in tiddlers that have either tag foo or tag bar. And be invisible for all other tiddlers. Is this possible? I always thought “conditional viewtemplate” did this, but it turns out it does not.

You can wrap the contents of the tiddler in a list widget so that it’s effectively only displayed when the filter condition is true:

<$list filter="[<currentTiddler>tag[foo]] ~[<currentTiddler>tag[bar]]">
... ViewTemplate contents ...
</$list>

I’m using the :else/~ filter run prefix here to make sure that the second condition is only tested if the tiddler doesn’t have the tag “foo”—this is important because otherwise, the template will display twice on any tiddler with both tags!

Here’s an alternate filter that achieves the same result; it may be more concise, especially if you want to add more tags to test. In this case, we’re using +[limit[1]] to prevent multiple display.

"foo bar [[multiword tag]] :intersection[<currentTiddler>tags[]] +[limit[1]]"
5 Likes

Sorry for the late reply. Thank you for this!

1 Like

Also, a recently introduced idiom for this use case consists in adding a filter to the ViewTemplateBody cascade.

Assuming that @DaveGifford’s View Template is named View Template for foos and bars, create a new configuration tiddler, tagged with $:/tags/ViewTemplateBodyFilter, with the following text field:

[tag[foo]] [tag[bar]] :and[then[View Template for foos and bars]]

To make sure that this case is handled in priority, add an empty list-before field.