Please help me improve this filter

I have this filter.

{{{ [<currentTiddler>has[caption]get[caption]] [<currentTiddler>!has[caption]get[title]] }}}

What I want is,

  1. If tiddler has caption field set, return it
  2. Else return the title field

The above filter works fine.

But I suspect it can be made shorted using then and else. But I could not figure out the correct syntax.

What have I tried?

{{{ [[demo04 1]has[caption]get[caption]else[get[title]]] }}} 

It returns error.


Related to accordion thread

1 Like

{{{ [<currentTiddler>] :map[get[caption]else{!!title}] }}}

See https://tiddlywiki.com/#Map%20Filter%20Run%20Prefix%20(Examples)

2 Likes

Other forms of this are lso commonly used;

{{{ [all[current]get[caption]else{!!title}] }}}

or without a link forming

<$text text={{{ [all[current]get[caption]else{!!title}] }}}/>

Or a list of working links even when caption used

<$list filter="[tag[TableOfContents]]">
<$link><$text text={{{ [all[current]get[caption]else{!!title}] }}}/></$link><br>
</$list>

Note: {!!title} is equivalent to [all[current]get[title]] or [all[current]]

1 Like

The TW uses the below simple code

<$view field=caption>
  <$view field=title/>
</$view>

This is semantic and simple.

2 Likes

Another version that is a bit shorter {{{ [{!!title}get[caption]else{!!title}] }}}

2 Likes

Thank you @saqimtiaz, @TW_Tones, @Mohammad and @telumire

Your response made me look up the documentation

The content of the <$view> widget is displayed if the field or property is missing or empty.

Great observation.

I think this following version can be improved further,

Just recently, I was building a select widget, which actually contains a list of items to select, and some of the “core” tiddlers have a caption that includes a transclusion of an icon/image tiddler, so you see {{imagetiddler}} caption.

Instead try this;

<$transclude field=caption>
  <$view field=title/>
</$transclude>
  • Transcluding the field effectively wikifies the caption if its present and if not, we view the title.
  • his works for existing core and custom buttons containing icons in the caption field
  • This introduces the ability to use wikitext in captions
  • Transclude also displays its content if blank
  • It would be not advisable to wikify Titles so it just uses $view

Of course this is the place, in a “list item” to introduce other features to lists such as list item buttons to operate on each item in the list.

2 Likes

The only issue here is $transclude cannot determine an empty field, so if the caption is existed and empty, then you see an empty output for that item!

I assume this is a core issue! You may need extra $list to determine if the transcluded field is empty or not!

  • Funny, Just tested it and its true, but I do not recall that behaviour.