How to Get Fields to Display in a Grid in a Tiddler

I am comfortable with markup, but I find myself a complete TW newbie when it comes to macros and javascript and such. Let’s say I have a bunch of fields in a Tiddler; this is for RPG campaign planning and maintenance, so let’s go with the standards of characteristics: Strength, Endurance, Intuition, Charm. How do I get the tiddler to display those as a grid, with “ST”, “EN”, “IN”, and “CH” along the top row, and the numbers along the bottom row? I can hardcode the numbers in markup, but I’d like to instead call the values from the fields in the tiddler.

Give this a try:

|! ST |! EN |! IN |! CH |
| {{!!strength}} | {{!!endurance}} | {{!!intuition}} | {{!!charm}} |

Notes:

  • The “vertical bars” are TiddlyWiki syntax for creating a simple table
  • In the first row, each cell begins with a “!” to automatically format it as a table heading
  • The spaces surrounding the cell content displays the content centered in the cell
  • In the second row, each cell uses the {{!!fieldname}} syntax to retrieve and display the values stored in the named fields from the current tiddler
  • see https://tiddlywiki.com/#Tables%20in%20WikiText for table formatting details

You can, of course, just copy/paste the above code into each character’s tiddler to display the table there. However, for greater convenience, you can define a “global macro” so you can easily add the formatted table to any tiddler without repeating the same table code over and over.

Start by creating a tiddler with any name you like (e.g., “ShowCharacteristics”). The specific name doesn’t matter… you could call it “Fred” or “ArgleBargle” if you like.

  • Set the tags field of the tiddler to $:/tags/Global
  • Enter the following macro definition containing the table formatting content into the text field of the tiddler:
\define character_table()
|! ST |! EN |! IN |! CH |
| {{!!strength}} | {{!!endurance}} | {{!!intuition}} | {{!!charm}} |
\end

Now, in any character tiddler, you can just write:

some text here

<<character_table>>

to render the character_table macro using the field values from that tiddler. Note that, if the table is preceded by other text content (e.g, “some text here”), you will need to include a blank line before invoking the <<character_table>> macro. This is required so that the table will be properly rendered in “block” mode instead of “inline” mode (see https://tiddlywiki.com/#Block%20Mode%20WikiText)

If you want to avoid the need for this blank line in each character tiddler, you can adjust the macro definition, like this:

\define character_table()
<div>

|! ST |! EN |! IN |! CH |
| {{!!strength}} | {{!!endurance}} | {{!!intuition}} | {{!!charm}} |
</div>
\end

This ensures that the blank line that indicates “block mode” formatting is automatically included by the macro itself. Then, in each character tiddler, you can just write:

some text here <<character_table>>

and the table will always start on a new line and be correctly formatted, regardless of the placement of the <<character_table>> macro invocation.

Hope this all makes sense. Let me know how it goes…

enjoy,
-e

2 Likes

Wow! That’s extremely helpful! Thank you so, so much! I was feeling like an idiot trying to figure out how to get the fields in there dynamically based on tags – it makes sense to automate that part of the process, too, so that if I put a tag there, say “character”, it automatically populates the fields with “strength”, “endurance”, “intuition”, and “charm”.

Thanks again, this is good stuff. My daughter’s two, and I feel like my brain is atrophying from lack of sleep and stimulation – learning this stuff is definitely helping with that second one! :smiley:

EDIT: And just to be clear, it works perfectly! Thank you so much!

Currently looking at @Mohammad’s excellent template plugin here: Edit Toolbar: Apply Template

Using this, I should be able to define a template for, say, a character, and I can put in your code above, and it should speed up the whole process! Fantastic! I’m not sure how I was running games before TW!

1 Like