Note: An alternative skin, TW4MW (work in progress), is available from the user preferences.

Dev:Extended Fields

TiddlyWiki Community Wiki

Jump to: navigation, search
This page's contents need to be reviewed and/or refactored to comply with TiddlyWiki.org's quality standards.
Please improve the article, or discuss the issue on the talk page.

TiddlyWiki 2.1 allows you to save arbitrary custom fields with your tiddlers. Here is a short introduction to using these fields.

Contents

[edit] Basics

Edit your ViewTemplate tiddler. Paste in the following after the stuff that's already there:

<div>My Thing: <span macro="view mything"></span></div>

Edit your EditTemplate tiddler. Paste in the following after the stuff that's already there:

<div>My Thing: <span macro="edit mything"></span></div>

That's all you need to do -- you've created a custom field. When you view a tiddler you should see the My Thing field, and when you edit a tiddler you can enter stuff in there and it will save with the tiddler.

Tiddler fields are currently case-sensitive and must be all lower case for their values to be stored/retrieved sucessfully.

If you want the field contents wikified when viewing (eg. so links works) add a space and the word 'wikified' after 'mything' (ViewTemplate only, not EditTemplate):

<div>My Thing: <span macro="view mything wikified"></span></div>

[edit] Changing the appearance of the field value

You can use styles to make the field appear however you want. Eg:

<div style="font-size:200%;">My Thing: <span macro="view mything"></span></div>

A more manageable way is to use a class and put the styles in your StyleSheet tiddler. For example:

<div class="mything">My Thing: <span macro="view mything"></span></div>

The put something like this your StyleSheet tiddler:

.mything {
  color:white;
  background: red;
  border:3px solid green;
}

If you are storing wiki format text in your field, you can ask for the wikifier to render the field contents by adding "wikified" to the macro call:

<span macro="view myThing wikified"></span>

Conditional display of field values can be achieved using the HideWhenPlugin.

[edit] Changing the size of the edit box

By default the edit boxes come out a certain size. If you need to change the size you can do it with CSS. For example:

<div><span class="smallerEdit" macro="edit mything"></span></div>

Then put something like this your StyleSheet tiddler:

.smallerEdit input {
 width:5em;
}

[edit] Using HideWhen to do conditional fields based on tags

(This requires that you install HideWhenPlugin from MonkeyPirateTiddlyWiki.)

Let suppose you have a some tiddlers tagged with "friend" and you'd like to use some custom field to maintain some information about them, say their favourite food.

In ViewTemplate:

<div macro="showWhen tiddler.tags.contains('friend')">
  Favourite food: <span macro="view favouritefood"></span>
</div>

In EditTemplate:

<div macro="showWhen tiddler.tags.contains('friend')">
  Favourite food: <span macro="edit favouritefood"></span>
</div>

Now you can view and edit your friends' favourite food.

[edit] Using forEachTiddler with tiddler fields

(This requires you install ForEachTiddlerPlugin from Abego Software.)

To list all your friends whose favourite food is pizza you can do this:

<<forEachTiddler
   where
      ' store.getValue(tiddler,"favouritefood") == "pizza" '
>>

This will display a bulleted list of tiddlers that match the test: store.getValue(tiddler,"favouritefood") == "pizza" (if you don't want a bulleted list, refer to the ForEachTiddler documentation for how to customise the output).

[edit] Using readBracketedList to make a list

Suppose you want to be able to store more than one favourite food. The best way to do this is to use the same format that TiddlyWiki uses for tags, ie, "this that [[the other]]" means three items, "this", "that" and "the other". There is a TiddlyWiki method called readBracketedList that we can use for this.

Now if we have at least one food in the custom field we can do this to see who likes pizza:

<<forEachTiddler
  where
    ' store.getValue(tiddler,"favouritefood") &&
      store.getValue(tiddler,"favouritefood").readBracketedList().contains("pizza") '
>>

Who likes chicken or fish?

<<forEachTiddler
  where
    ' store.getValue(tiddler,"favouritefood") &&
      store.getValue(tiddler,"favouritefood").readBracketedList().containsAny(["chicken","fish"]) '
>>

Who likes olives and anchovies?

<<forEachTiddler
  where
    ' store.getValue(tiddler,"favouritefood") &&
      store.getValue(tiddler,"favouritefood").readBracketedList().containsAll(["olives","anchovies"]) '
>>


The methods "contains", "containsAny" and "containsAll" are handy Array methods defined by TiddlyWiki. And "readBracketedList" is a String method that is used to convert a tags string into an Array. The reason we have to first test if the value is defined is that otherwise readBracketedList will fail on tiddlers that don't have a favouriteFood field.

Please note that the above 3 contain Array methods are case sensitive, and will only find entire words: in the above example, "olives" will be found, "olive", "OLIVE", or "Olives" will return no results. An asterisk (*) used as a wildcard does not work for these contain methods.

[edit] Filtering based on more than one field

Provided you had set up another custom field "favouritedrink" per the above instructions, the following should allow you to further filter based on more than one field:

Who likes olives and coffee?

<<forEachTiddler
  where
    ' store.getValue(tiddler,"favouritefood") &&
      store.getValue(tiddler,"favouritefood").readBracketedList().contains("olives") &&
      store.getValue(tiddler,"favoritedrink") &&
      store.getValue(tiddler,"favouritedrink").readBracketedList().contains("coffee")'
>>

[edit] See Also

Personal tools