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.

1 Like

Just used this. Instead of tagging your custom template tiddler with $:/tags/ViewTemplate, you leave that tiddler untagged and you make a second tiddler with the filter selection designating which tiddlers should be rendered with this template, and that tiddler is tagged $:/tags/ViewTemplateBodyFilter. It seems better than using a <$list> widget in a template that is applied to every tiddler.

IMO, these are both good solutions for slightly different use-cases.

A tiddler with the $:/tags/ViewTemplate tag will be rendered in addition to the default tiddler body template $:/core/ui/ViewTemplate/body/default (which is responsible for displaying either the text of the tiddler or the missing tiddler hint).

  • It can be repositioned relative to other segments of the tiddler like the title/subtitle/tags by dragging it in the $:/tags/ViewTemplate tag-pill.

A tiddler configured to display via the ViewTemplateBody cascade will replace the default tiddler body.

  • If you still want to see the text field content of that tiddler, you’ll need to include <$transclude /> somewhere in your custom template.
  • Your custom template also takes the place of the default body template relative to other ViewTemplate segments; you cannot change where it appears in the segment stack without moving the default body template as well.
  • You need to set the relative priorities of any template filters you’ve added to the cascade either by using the list-before/list-after fields (as @xcazin suggested) or by dragging them into the appropriate position in the $:/tags/ViewTemplateBodyFilter tag-pill (available at the top of each Cascades tab).
    • It is crucial that your filter appears before $:/config/ViewTemplateBodyFilters/default (and any other core templates which might share the same filter output) or it will not appear at all.
2 Likes