macro for 'if field exists then A else B else C' ???

304 views
Skip to first unread message

Glenn Dixon

unread,
Jan 3, 2021, 7:32:35 AM1/3/21
to TiddlyWiki

I have a working macro, but just realized that the field it uses is sometimes not present. There is an alternate field I would like to use in its place. I also need to use a 'dummy' result if *both* fields are not present.

My dozens of searches through TW documentation and this group have so far proven futile. Could use some help...

Saq Imtiaz

unread,
Jan 3, 2021, 8:09:24 AM1/3/21
to TiddlyWiki
Sounds like something I would do either with filters use the else[] operator, or with nested <$view> widgets (where the content of a view widget is only displayed if the target field is absent).

https://tiddlywiki.com/#ViewWidget

Hard to be more specific without seeing your code.

Glenn Dixon

unread,
Jan 3, 2021, 8:34:53 AM1/3/21
to TiddlyWiki
My brain is obviously incapable of understanding TW documentation. Any way, here is the code


<$macrocall $name="makeimg" isbn={{!!isbn}} />

I need to basically add options for "if no isbn, use isbn13. If neither, then use this generic book cover image"

isbn and isbn13 are both fields - when I imported, if the field was empty, the field was not set up in the tiddler, so it's not a blank field, it's a non-existent field. Half my books have one or the other, some have both, some have neither.

Something like that...

Saq Imtiaz

unread,
Jan 3, 2021, 9:35:35 AM1/3/21
to TiddlyWiki
One option would be to call your macro like this: 
<$macrocall $name="makeimg" isbn={{{ [{!!isbn}!is[blank]else{!!isbn13}] }}} />

This would be simpler:

\define makeimg()
<$vars isbn={{{[{!!isbn}!is[blank]else{!!isbn13}]}}}>
</$vars>
\end

<<makeimg>>

For the generic book image fallback it depends on whether that is also a remote image or a local one.

Saq Imtiaz

unread,
Jan 3, 2021, 9:38:38 AM1/3/21
to TiddlyWiki
Here is an example on falling back to the generic image if no ISBN fields are defined

\define makeimg()
<$vars isbn={{{[{!!isbn}!is[blank]else{!!isbn13}]}}}>
<$image source={{{ [[http://covers.librarything.com/devkey/dev-key-goes-here/medium/isbn/]addsuffix<isbn>] :filter[<isbn>!is[blank]] :else[[generic image URL]] }}}/>
</$vars>
\end

<<makeimg>>

Glenn Dixon

unread,
Jan 3, 2021, 9:59:03 AM1/3/21
to TiddlyWiki
This looks nice! It appears to put all the right code in the right place. Now if only librarything servers would quit dying on me...

Are there any other widgets with :filter and :else options that I could use to do something similar w/ a link? Specifically, use isbn or isbn13 in link, but if they don't exist show this *other* link? I've been using [ext[ link format, but need to do some if/then...

Saq Imtiaz

unread,
Jan 3, 2021, 10:08:08 AM1/3/21
to TiddlyWiki
The last example does that, it falls back to an entirely different URL if no ISBN fields exist. 

Glenn Dixon

unread,
Jan 3, 2021, 10:10:44 AM1/3/21
to TiddlyWiki
Yes, but I need a plain URL, not an image link, so wouldn't be able to use <$image ...

Saq Imtiaz

unread,
Jan 3, 2021, 12:20:57 PM1/3/21
to TiddlyWiki
Wrap the image part in a <$list> widget that tests whether isbn is blank. You can use the emptyMessage attribute to show the plain URL instead when there is no isbn.

Glenn Dixon

unread,
Jan 3, 2021, 2:39:18 PM1/3/21
to TiddlyWiki
Thank you very much for your help!

I guess I need to give up on this particular idea. There are only so many hours I can spend struggling with something like this before I am simply ready to ditch TiddlyWiki altogether. I really struggle with the syntax. Depending on the context, I may need one, two or three curly braces, or maybe '<' - or maybe '<<' - and I can't find any rhyme or reason for these contexts. The documents seem to be written for someone familiar with multiple programming languages. That's not me.

I just need a simple if/then/else and that seems to require a lot of strange workarounds. I guess I'll look for that elsewhere...
Message has been deleted

Glenn Dixon

unread,
Jan 4, 2021, 9:15:34 AM1/4/21
to TiddlyWiki
Thanks, but I've moved my book collection back online to LibraryThings.

On Monday, January 4, 2021 at 5:10:46 AM UTC-6 amreus wrote:
Hopefully this is helpful.  I also find the syntax arcane and still struggle with constructing code like the following.

\define makeimg()
<$vars
  imgurl="http://covers.librarything.com/devkey/dev-key/medium/isbn/"
  fallback="[[Normal Link|http://google.com]]"

  isbn={{{[{!!isbn}!is[blank]else{!!isbn13}]}}}
>
<$list filter=<<isbn>> emptyMessage=<<fallback>>>
  <$image source={{{[<imgurl>addsuffix<isbn>]}}}/>
</$list>
</$vars>
\end

<<makeimg>>

TW Tones

unread,
Jan 5, 2021, 5:55:27 PM1/5/21
to TiddlyWiki
Dix,

Since you mentioned it I had a look at LibraryThing, and started cataloguing my books found in different locations "Rooms, shelves" since the ISBN scanner is easy to use.

Have you exported LibraryThing data? Eventually I may move this data into tiddlywiki.

I am, off to our "shack in the bush" where I am am keen to record what is there, but in time I would like to make my own "little library" application on tiddlywiki,  so I can keep track of books moving between locations, needing reading and lending to friends. The apps out there never quite focus on what I want.

I have in the past use plugins and macros to link ISBN's to books and their metadata in tiddlywiki.

Perhaps we should start a subject specific thread for libraries in TiddlyWiki.

Regards
Tones

Glenn Dixon

unread,
Jan 5, 2021, 6:10:44 PM1/5/21
to TiddlyWiki
On Tuesday, January 5, 2021 at 5:55:27 PM UTC-5 TW Tones wrote:
Dix,

Since you mentioned it I had a look at LibraryThing, and started cataloguing my books found in different locations "Rooms, shelves" since the ISBN scanner is easy to use.

Have you exported LibraryThing data? Eventually I may move this data into tiddlywiki.

You can export to Excel, tab-delimited, JSON and Marc data (whatever that is)


Perhaps we should start a subject specific thread for libraries in TiddlyWiki.

Regards
Tones

I would be interested still! :)
Reply all
Reply to author
Forward
0 new messages