Reading tiddler fields in JavaScript

110 views
Skip to first unread message

KRM

unread,
Nov 19, 2018, 9:31:28 PM11/19/18
to TiddlyWiki
It is nice and simple to transclude an arbitrary tiddler's field directly into WikiText or to use the view widget. But I'm having difficulty cracking how to do the same thing inside a JavaScript function tiddler! I couldn't dig up any pertinent documentation and quickly pawing through some $;/core macro source also failed to provide me instant gratification. Can someone point me in the right direction here? Presumably something vaguely resembling:

var target_field_value = this.wiki.getTiddler(target_tiddler_name).getFieldValue(target_field_name);

Obligatory -- I've been using TiddlyWiki for over a decade (although until recently my use has always been super-vanilla without any substantial customization) and it is just the absolute best note-knowledge-graph-data-store-outlining-to-do-thing-a-ma-jig out there. I've just recently started serving my TiddlyWikis with Node.js on my LAN and the painless, invisible automation of the saving process has finally killed the only significant irritation I've had with TiddlyWiki in all this time.

TonyM

unread,
Nov 19, 2018, 11:27:49 PM11/19/18
to TiddlyWiki
KRM,


Not sure it has your answer but worth a try.

Tony

Jed Carty

unread,
Nov 20, 2018, 7:21:14 AM11/20/18
to TiddlyWiki
You almost have it, the function is getFieldString, not getFieldValue. If you replace the function name in your example it should work as expected.

If you want some more in-depth information, here it is:

A tiddler is just a special type of javascript object. The object returned by this.wiki.getTiddler(tiddlerTitle) is in the from:

{fields: {
  title
: tiddlerTitle,
  text
: some text,
  tags
: tag
 
},
 cache
: {}
}


So to get the field values you use something like this:

var tiddler = this.wiki.getTiddler(tiddlerName)
var fieldValue = tiddler.fields.fieldname

where you replace fieldName with the actual name of the field. To fit with javascript syntax you may need to reference field names like this tiddler.fields['field-name'] when there is a - or a . in the field name.

The getFieldString function has some extra steps so an empty string is returned in the case of missing fields or invalid field values, and it uses the build-in stringifying functions for fields like the tags field.

KRM

unread,
Nov 20, 2018, 12:09:44 PM11/20/18
to TiddlyWiki
Thanks, that does the trick! Appreciate the fast response.
Reply all
Reply to author
Forward
0 new messages