How to show N symbols based on numeric tiddler field value

89 views
Skip to first unread message

Jack Baty

unread,
May 10, 2021, 10:33:36 AM5/10/21
to TiddlyWiki
I have a "rating" field that contains a number from 1-5.

When generating a table (or list) of rated tiddlers, I want to display the rating as a number of stars. e.g. ★★ or ★★★ etc corresponding to the value in rating for each tiddler.

For the life of me, I can't figure out how to do this. Is there some form of string math like "★*rating"? Do I need to choose from a pre-defined list? Do I need to just give up and change the field to contain the actual stars? I'm stumped, sorry.

Jack




Mohammad Rahmani

unread,
May 10, 2021, 10:52:34 AM5/10/21
to tiddl...@googlegroups.com
Hi Jack,
 The Vote plugin from Kooma Library do rating and you can retrieve stars and compare and analyse!

But if you want your simplest solution the below is one! Wait for better, I sure you will get alternatives


\define getStars() <$list filter="[range[1,$(vl-stars)$]]">*</$list>

<$list filter="[all[current]has[rating]get[rating]]" variable="vl-stars">
<<getStars>>
</$list>

The above can be used inside a vietemplate if you like to have stars automatically displayed!
Adopt if you like to use a table! for a specific tiddler!




Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/cc78aa80-95c1-4608-aad8-f41c4d2959b9n%40googlegroups.com.

Jack Baty

unread,
May 10, 2021, 2:49:33 PM5/10/21
to TiddlyWiki
Mohammad,

Thank you! Your sample helps me understand how to do this kind of rendering, though, which is great. (I still seem to overlook using $list for seemingly everything). 

I'm using Tobias' rate plugin for setting and displaying the ratings on individual tiddlers and it works well. 

I'm now working on getting the macro to run correctly when used in a template in the TiddlyTables plugin. I've tried every combination I can think of in the column's view template but can't get the macro to "see" the rating value for each row. If I render a table "by hand" it works fine.

Maybe someone can spot an obvious error in my implementation here:


There's a custom template for rating that contains the following:

```
<td>
<$macrocall $name="getStars" numstars={{!!rating}} />
</td>
```
I feel like I'm going to need "<<currentRecord>>"  somewhere but can't seem to figure out where.

Anyway, thanks for the leg up. I can fall back to my hand-rendered table easy enough if I can't figure out how to use it in TiddlyTables.

Soren Bjornstad

unread,
May 10, 2021, 3:04:04 PM5/10/21
to TiddlyWiki
I think you want:

```
<td>
<$macrocall $name="getStars" numstars={{{ [<currentRecord>get[rating]] }}} />
</td>
```

Worked for me in the wiki you linked.

Also, it looks like you've got this figured out, but I have an example of an implementation of a star rating in one of my YouTube videos if you're curious:

Jack Baty

unread,
May 10, 2021, 3:13:32 PM5/10/21
to TiddlyWiki
Well look at that, it works! I'm not sure I would have ever gotten there on my own, so thank you.

It's just single-bracket macro calls in a filter inside a "super" transclusion as a macro parameter. Simple! 😝. I'll understand all this, eventually, I hope.

Thanks for the video link. I've been devouring your videos but must have forgotten about the ratings portion of that one. I'll rewatch it now.

Jack

Mohammad Rahmani

unread,
May 10, 2021, 3:15:02 PM5/10/21
to tiddl...@googlegroups.com
Hi Jack,
 If you want to keep your own solution then go with Soren! 
What Soren provided shows the trick!

Another solution is to  change the content of your template (https://rudimentarylathe.wiki/#%24%3A%2Fplugins%2Faaldrich%2Ftables%2Ftemplate%2Fbody%2Frate)
with the below wikitext

\define ratingFilter() [range[1,$(rating)$]]

<$list filter="[<currentRecord>has[rating]get[rating]]" variable=rating emptyMessage="""<td></td>""">
<td>
<$list filter=<<ratingFilter>> > {{$:/core/images/star-filled}}</$list>
</td>



Also in the example https://rudimentarylathe.wiki/#My%20Books%20-%20TEST clear extra wikitext, delete the macro getSras and just

Working on a better way to render the list of books.

Here's a failed attempt using ~TiddlyTables:

<<table>>



Tested on your wiki, I get the below result

image.png



Best wishes
Mohammad


Jack Baty

unread,
May 10, 2021, 3:35:31 PM5/10/21
to TiddlyWiki
Mohammad, 

This also solves the thing I was just working on, and that is hiding stars when there is no rating. Nice!

Thanks to both of you for helping with this. I've got so much to learn.

Jack



TW Tones

unread,
May 10, 2021, 8:48:57 PM5/10/21
to TiddlyWiki
jack,

In my view the list widget is the core of tiddlywiki 5 as the For Each Tiddler plugin for for tiddlywiki classic. The world and tiddlywiki can be handled using lists or sets so do trust in the fact that filters and the list widget are the most essential tiddlywiki constructs.

But given the shorter form is the triple curly braces, or "filtered transclusions",  this is becoming a popular approach in many solutions, have a close look at that. See my reference here

I have being involved with tiddlywiki for more than a decade, if that helps add a little authority to my assertions.

Regards
Tones

Reply all
Reply to author
Forward
0 new messages