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

Dev:Macro Parameters

TiddlyWiki Community Wiki

Jump to: navigation, search
This page is still incomplete. You can help by contributing to its expansion.
Please improve the article, or discuss the issue on the talk page.

Contents

[edit] Unnamed Parameters

<<foo value1 value2 value3>>

With the example macro call above, the params variable passed to the macro's handler is an array, created by parsing the space-separated list of strings. For multi-word parameters, use a param.

Example:

config.macros.myMacro.handler = function(place,macroName,params,...) {
   if(params[0])
      var length = params[0];
   if(params[1])
      var width = params[1];
...

[edit] Named Parameters

by Jeremy

The semantics of parseParams() are that either defaultName or defaultValue can be provided. If defaultName is provided, then any single token parameters are taken to be values, with the name being specified in defaultName. If instead defaultValue is defined, then any single token parameters are taken to be names, with the value being specified in defaultValue.

There is a potential error condition if neither defaultName or defaultValue is provided, but this only occurs if the parameter string in question includes any single token values. The current patch incorrectly treats a missing defaultName as an error.

Code for parsing named parameters:

config.macros.foo.handler = function(place, macroName, params, wikifier, paramString) {
	var prms = paramString.parseParams(null, null, true);
	var value = getParam(prms, "key");
};

To be used like so:

<<foo key:value>>
<<foo key:"value">>
<<foo key:[[value]]>>

[edit] Default Names

The first (required) parameter of parseParams() will be used as the default name for unnamed parameters:

p = paramString.parseParams("anon", null, true);

Usage:

<<foo value1 value2 value3>>

In this example, anon is used as the default name when processing parameters.

[edit] Default Values

If you set the second parameter of parseParams but not the first, that will be taken as the default value for named parameters without values:

p = paramString.parseParams(null, "val", true);

Usage:

<<foo p1:A p2:B p3: p4:D>>

In this example, val is used as the default value when processing parameters.

Personal tools