Passing Named Parameters to a Macro

200 views
Skip to first unread message

Scott Simmons

unread,
Jun 26, 2011, 11:08:54 AM6/26/11
to tiddl...@googlegroups.com
Hi, all —
 
Can anyone tell me how to pass named parameters into a macro and use them to define variables?  What I'm looking to do is define a macro like so:
 
<<macroName parameter1:"value1" parameter2:"value2" parameter3:"value3">>
 
And then use that values in my macro, as JS variables.
 
I've tried:
 
var variable1 = getParam(params,"parameter1","");
 
... to no avail.  I suspect there's a simple syntax error at play, but I can't tell what it is.
 
Does anyone have a moment to set me straight on how the getParam function works — or point me toward some helpful documentation?  :)

Eric Shulman

unread,
Jun 26, 2011, 2:05:43 PM6/26/11
to TiddlyWiki
> Can anyone tell me how to pass named parameters into a macro and use them to
> define variables?  What I'm looking to do is define a macro like so:
> > <<macroName parameter1:"value1" parameter2:"value2" parameter3:"value3">>
> And then use that values in my macro, as JS variables.
> > var variable1 = getParam(params,"parameter1","");
> ... to no avail.  I suspect there's a simple syntax error at play, but I
> can't tell what it is.
> Does anyone have a moment to set me straight on how the getParam function
> works — or point me toward some helpful documentation?  :)

The arguments to the macro's handler function including *two* ways to
access the macro parameters:

1) 'params' is an array of text strings. It is generated by the
TWCore macro processor by applying the .readBracketedList() method to
the text of the space-separated macro parameters. Thus, the items in
the params array are simple *positional* parameters. For example:
<<macroName foo bar baz mumble>>
results in:
params[0]="foo"
params[1]="bar"
params[2]="baz"
params[3]="mumble"

2) 'paramString' is a single piece of text containing the complete
parameter text, exactly as entered into the macro:
<<macroName foo bar baz mumble>>
results in:
paramString="foo bar baz mumble"

Note that in (1), if you use a *named* parameter, e.g.:
<<macroName foo bar something:baz mumble>>
the result is:
params[0]="foo"
params[1]="bar"
params[2]="something:baz"
params[3]="mumble"
i.e., the *name* is NOT processed separately as is treated as part of
the value. To get the *named* parameter values, you need to run the
paramString through a method called .parseParams:
var parsed=paramString.parseParams('anon',null,true,false,false);

This results in a structured object containing information about the
parameters. Then, to extracdt a specific parameter value by name, you
write:
var myVar=getParam(parsed,'paramname','defaultvalue');

enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios
----------
Was this answer useful? If so, please help support TiddlyTools:

TiddlyTools direct contributions: (paypal)
http://www.TiddlyTools.com/#Donate
UnaMesa tax-deductible contributions:
http://about.unamesa.org/Participate (paypal)
TiddlyWiki consulting:
http://www.TiddlyTools.com/#ELSDesignStudios
http://www.TiddlyTools.com/#Contact



Instead, to extract *named* parameters, you use the paramString

HansBKK

unread,
Jun 26, 2011, 2:14:30 PM6/26/11
to tiddl...@googlegroups.com
Sorry if this is off-topic, but I asked here
https://groups.google.com/forum/#!topic/tiddlywiki/nedSdsCE4eo
and didn't get a response - feel free to respond there rather than here if that's appropriate. . .

How can I pass in the title of the current tiddler to a macro?

And would this be the same if it's part of the tiddler's content, compared to inside a div in the template?

PMario

unread,
Jun 26, 2011, 4:42:53 PM6/26/11
to TiddlyWiki
Hi Scott,
I made some "helloWorld" examples. In addition to Erics info it may be
of use.

From some to many params :)

http://helloworld.tiddlyspace.com/#HelloWorldButtonPlugin
http://helloworld.tiddlyspace.com/#HelloWorldNamedParamsPlugin
http://helloworld.tiddlyspace.com/#HelloWorldManyParamsPlugin

have fun!
mario

Scott Simmons

unread,
Jun 28, 2011, 2:09:07 PM6/28/11
to tiddl...@googlegroups.com
I love you guys!  >sniff<
 
Mario, you are the king of test cases and example code.  I've snatched all three of those tiddlers for future reference, as well as the main HelloWorldPlugin that indexes 'em.  There's nothing like an empty example to work from.
 
And Eric, once again you've proven that you can explain the simple as well as the mind-bogglingly complex.
 
Hans, I look into your original thread during my search but (alas) wasn't able to help.  It looks like you've got some of the resident TW gurus on the case now, though, so I wish you luck!
 
If anyone's curious what I was up to, you can take a look here:
 
 
This is something I cooked up in a rudimentary form, before Tobias published his super-swanky twYp plugin (http://twyp.tiddlyspace.com/#twyp) — and I've continued tinkering with it to brush up on my sad-sack JavaScript and learn more about plugins.  Who knows — maybe the simplicity will have some appeal for someone else who wants a quick-and-easy way to drop YouTube videos into their tiddlers.  :)

PMario

unread,
Jun 28, 2011, 4:31:51 PM6/28/11
to TiddlyWiki
>If anyone's curious what I was up to, you can take a look here:
>http://tiddlywiki.secret-hq.com/#YouTubeVideoDisplayPlugin
Nice work :)

I think, you could make your shadow tiddler creation a little bit
easier, and smaller :)
http://pmario.tiddlyspace.com/#%5B%5BFour%20Versions%20to%20create%20a%20shadow%20tiddler%5D%5D
-m

Scott Simmons

unread,
Jun 29, 2011, 11:20:04 AM6/29/11
to tiddl...@googlegroups.com
You're too kind to my goofball little plugin, Mario — but thanks!  :)
 
Those shadow tiddler tips are great, too.  It never occurred to me to approach them that way.  In my first efforts, I wrote each shadow tiddler in Notepad and whipped up a macro to JS-ify them and escape my quotation marks -- but when I lost that macro, I fell back on compiling them line-by-line like you see in my YouTube player plugin.
 
Actually, since you guys have been so great, I'll start another thread with another question that arose in putting this one together ... .  ;)
Reply all
Reply to author
Forward
0 new messages