Simple, one-off number comparison

50 views
Skip to first unread message

Antonis Lamnatos

unread,
Mar 18, 2021, 5:55:41 AM3/18/21
to TiddlyWiki
Hello everyone, first-time poster here :-)

I'm trying to output the number of books an Author has written. I want to end up with the following:

Author X has no books
or
Author X has 1 book
or
Author X has n books

After searching around, I thought I'd try the `<$reveal>` widget to compare a number returned by a `<$filter count>`. Here's what I have on each Author's page by using a `{{||template}}`, each book page has an `author` field with the author's name:

```
<$set name="bookCount" filter="[author{!!title}count[]]">
<$reveal type="eq" default=<<bookCount>> text=0>
No books
</$reveal>
<$reveal type="eq" default=<<bookCount>> text=1>
<<bookCount>> book
</$reveal>
<$reveal type="gt" default=<<bookCount>> text=1>
<<bookCount>> books
</$reveal>
</$set>
```

The above works but only for more books than 1 (the final, `gt` clause). If it's 0 or 1 books, nothing is returned.

What am I missing?

I'm on TW 5.1.23, running through Node.js and I'm using Firefox and Edge.

Eric Shulman

unread,
Mar 18, 2021, 7:21:19 AM3/18/21
to TiddlyWiki
On Thursday, March 18, 2021 at 2:55:41 AM UTC-7 lamn...@gmail.com wrote:
<$set name="bookCount" filter="[author{!!title}count[]]">
<$reveal type="eq" default=<<bookCount>> text=0> No books </$reveal>
<$reveal type="eq" default=<<bookCount>> text=1> <<bookCount>> book </$reveal>
<$reveal type="gt" default=<<bookCount>> text=1> <<bookCount>> books </$reveal>
</$set>
The above works but only for more books than 1 (the final, `gt` clause). If it's 0 or 1 books, nothing is returned.
What am I missing?

Use type="match", not type="eq"

Also, here's an alternative way to get the same output, using "inline filters":
<$set name="bookCount" filter="[author{!!title}count[]]">
<$text text={{{ [<bookCount>match[0]then[No]else<bookCount>] }}} />
<$text text={{{ [<bookCount>!match[1]then[books]else[book]]  }}} />
</$set>

enjoy,
-e

Antonis Lamnatos

unread,
Mar 18, 2021, 7:57:53 AM3/18/21
to TiddlyWiki
That worked beautifully, thank you!

I also liked the alternative, terser way of doing things. So, thank you for both bits.
Reply all
Reply to author
Forward
0 new messages