Passing a value with spaces into a macro

485 views
Skip to first unread message

David Allen

unread,
Feb 4, 2016, 5:42:43 PM2/4/16
to TiddlyWiki
This is an issue I seem to be having a lot, so I'm trying to find a solution.
I have the following macro:

\define ezlink(input)
<$link to=$input$>$input$</$link>
\end

If I were to do this:

<<ezlink {{!!title}}>>

and the tiddler it's used in has spaces in the title, (e.g. Hello World), the result would be a link to the tiddler Hello, using the text Hello World.

Anyone know what's going on with this or how to fix it so the ezlink macro goes to Hello World instead of just Hello?

David Allen

unread,
Feb 4, 2016, 5:58:35 PM2/4/16
to TiddlyWiki
Actually, let me correct that.  What I showed is actually working fine.  What's not working is when I try to pass a variable created by a list widget into a macro when the tiddler stored in currentTiddler contains spaces in its title.  I'm doing the following:

<$macrocall $name="ezlink" input=<<currentTiddler>>/>

Eric Shulman

unread,
Feb 4, 2016, 8:34:38 PM2/4/16
to TiddlyWiki
On Thursday, February 4, 2016 at 2:58:35 PM UTC-8, David Allen wrote:
Actually, let me correct that.  What I showed is actually working fine.  What's not working is when I try to pass a variable created by a list widget into a macro when the tiddler stored in currentTiddler contains spaces in its title.  I'm doing the following:

<$macrocall $name="ezlink" input=<<currentTiddler>>/>

\define ezlink(input)
<$link to=$input$>$input$</$link>
\end

summary:
   you need to add quotes around $input$ when used as a parameter in a widget...
details:

In the $macrocall, you correctly set input=<<currentTiddler>> ... where the <<...>> syntax retrieves the value from the variable.  However, when the macro is invoked, any embedded variables (e.g., $input$) are replaced by the corresponding value passed to the macro.  Thus, in your macro, if value of the input param is FOO, then the macro output is
<$link to=FOO>FOO</$link>

In contrast, if the input value is FOO BAR, then the macro output is
<$link to=FOO BAR>FOO BAR</$link>

Notice that, although the *text* of the link is FOO BAR, the parameter in the $link widget only recognizes the value FOO (i.e., up to the first space).  The remainder of the value is simply treated as another widget param -- one that is badly formed, as it doesn't follow the name="value" (or name=<<variable>>... or name={{reference}}) syntax.

As noted above, the fix is to add quotes when assembling the $link widget params, like this:
<$link to="""$input$""">$input$</$link>

This ensures that the resulting output encloses the parameter so it is treated as a single value that contains quotes, rather than two separate params; i.e.,
<$link to="""FOO BAR""">FOO BAR</$link>

Note that I've used the *tripled* quotes syntax around the param value (a TW5 extension to the HTML syntax).  This ensures that if the param value contains a regular quote ("), it won't be treated as the ending delimiter for the value.  i.e., the following syntax:
<$link to="$input$">$input$</$link>

would result in an invalid result when passed FOO"BAR:
<$link to="FOO"BAR">FOO"BAR</$link>

Notice how the embedded quote following FOO *ends* the parameter value, leaving BAR" as invalid junk, but when triple-quoting, the result would be
<$link to="""FOO"BAR""">FOO"BAR</$link>

which allows the TW5 core to treat the embedded quote as just a regular character.  QED.

enjoy,
-e
Eric Shulman
TiddlyTools  / ELS Design Studios
InsideTiddlyWiki: The Missing Manuals

David Allen

unread,
Feb 5, 2016, 3:49:11 AM2/5/16
to tiddl...@googlegroups.com
That worked for that situation, but now isn't working for another.  When I pass a text reference, say {{!!faction}}, it links to a tiddler called {{!!faction}}, instead of a tiddler whose title matches the faction field.
This email has been sent from a virus-free computer protected by Avast.
www.avast.com

--
You received this message because you are subscribed to a topic in the Google Groups "TiddlyWiki" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tiddlywiki/5xeFXiAMVZE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/7120e4ff-2be2-42c0-b04c-a0a8b78d8783%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
David Allen

Tobias Beer

unread,
Feb 5, 2016, 9:03:35 PM2/5/16
to TiddlyWiki
Hi David,
 
That worked for that situation, but now isn't working for another.  When I pass a text reference, say {{!!faction}}, it links to a tiddler called {{!!faction}}, instead of a tiddler whose title matches the faction field.

To pass the wikified results of a text-reference,
you need to use the macrocall widget.
Alternatively, you can use tobibeer/setvars.

Best wishes,

Tobias. 

Scott Simmons (Secret-HQ)

unread,
Feb 6, 2016, 6:27:56 PM2/6/16
to TiddlyWiki
Hi, David —

I highly recommend Tobias's setvars plugin.  It's gotten me through several similar situations where I needed to evaluate or wikify field contents for use in a macro.

As long as you're passing in a transclusion string (e.g., {{!!faction}}), you can use this sort of sytnax:

\define macro(param)
<$setvars _myparam=$param$ myparam="myparam">
Do something cool with <<myparam>>
</$setvars>
\end

... and <$setvars> will fetch the contents of the field (faction) into _myparam attribute, then create a variable, myparam, that you can use throughout rest of the macro (up until you hit </$setvars>).  If you field contains spaces, they'll be bound up as a single unit in the myparam value your macro uses to work its magic.

Tobias pointed me to it when I was trying to set "fallback" values for some of my macros.  You may be interested in the three tiddlers here:

Reply all
Reply to author
Forward
0 new messages