passing parameters to macro

111 views
Skip to first unread message

Bob Jansen

unread,
Sep 15, 2020, 8:44:56 AM9/15/20
to TiddlyWiki
How can I check if a parameter passed to a macro is blank?

I have tried

\define purchased(name_id)
<$list filter="[tag[Artworks]!tag[Index]!<currentTiddler>search:name_id{[<<__name_id__>>]is[blank]then[XX]}sort[title]]}>&bull; <$link to={{!!artwork_id}}><$view field="title"/> <$view field="artwork_title"/></$link><br/></$list>
\end

but this does not work. If name_id is empty I want to search to look for the string XX which I know doesn't exist.

bobj

Eric Shulman

unread,
Sep 15, 2020, 11:30:25 AM9/15/20
to TiddlyWiki
First, as you may have realized, the above doesn't work because you can't "nest" filter syntax (i.e., you can't directly use a filter to describe another filter's operand value)

To accomplish your goal, first calculate the desired operand value and assign it to a variable.  Then reference the variable in the filter, like this:
\define purchased(name_id)
<$vars id={{{ [[$name_id$]!is[blank]else[XX]] }}}>
<$list filter="[tag[Artworks]!tag[Index]!match<currentTiddler>search:name_id<id>]">
   
&bull; <$link to={{!!artwork_id}}><$view field="title"> <$view field="artwork_title"/></$link><br/>
<$list>
</$vars>
\end

Alternatively, since you know that searching for [XX] will result in no matching tiddlers, you could test for the blank value and just skip the $list entirely:
\define purchased(name_id)
<$list filter="[[$name_id$]!is[blank]]" variable="not_blank">
   
<$list filter="[tag[Artworks]!tag[Index]!match<currentTiddler>search:name_id[$name_id$]]">
     
&bull; <$link to={{!!artwork_id}}><$view field="title"> <$view field="artwork_title"/></$link><br/>
   
<$list>
</$list>
\end
The outer filter is being used as a conditional ("if name_id is not blank") with the variable ("not_blank") used to avoid changing the value of <<currentTiddler>>.
Note: I like to use a variable name that summarizes the purpose of the filter, but you could use any variable name (e.g., "foo") since you really don't reference it anywhere.  

-e

Dr Bob Jansen

unread,
Sep 15, 2020, 6:00:33 PM9/15/20
to tiddl...@googlegroups.com
Eric,

Thanks for your reply.

One question. You reference the macro parameter using multiple braces but the documentation says to use <<__...__>>. Is this documented somewhere?

BobJ

---------------
Dr Bob Jansen
The Cultural Conversations project 
Turtle Lane Studios Pty Ltd trading as the Australian Centre for Oral History
122 Cameron St, Rockdale NSW 2216, Australia 
Ph (Korea): +82 10-4494-0328 
Ph (Australia) +61 414 297 448 
Resume: http://au.linkedin.com/in/bobjan 
Skype: bobjtls 
KakaoTalk: bobjtls 
http://www.cultconv.com 

 In line with the Australian anti-spam legislation, if you wish to receive no further email from me, please send me an email with the subject "No Spam"
--
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/naMx9faSnRI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/ae75c768-803c-4bbb-9d71-20646a542e05o%40googlegroups.com.

TW Tones

unread,
Sep 15, 2020, 7:36:07 PM9/15/20
to TiddlyWiki
Bob,

My first observation from your post is the filter contains;
  • id{[<<__name_id__>>]is[blank]then[XX]}sort[title]]}
Which seems to include multiple syntax issues. 

Something that took me years to learn which may help if you is;

In wiki text we can also use html which uses <htmltag></htmltag> so to allow macros we use <<macroname>> and for widgets we use <$widgetname> for transclusion we use {{ tiddlername }}

When you use a filter, where html is not possible, you do not need to differentiate between <htmltag> and <<macroname>>, in fact both are illegal in filters,  etc.. so we remove a layer of brackets
The following is appropriate in a filter
  • <varname>  (not you can use <macroname> but not <macroname params> in filters
  • {tiddlername} returns content in tiddlername
  • {!!fieldname} returns content in currenttiddlers fieldname
  • Because a set of brackets already delimits a varname Rather than operator[<varname>] we use operator<varname>
  • Keep in mind we can use substitutions in filters $param$ and $(varname)$  $(Not retested recently by me)$ however (substitutions need not contain delimiters) so we use operator[$varname$]


<<__...__>>Parameter-as-variable access to a parameter defined in the macro parameters list

There are a few cases where using a parameter named in the \define pragma, normally in the form $parameter$, is not what you need
  • which simply substitutes $parameter$ with the value in the parameter, eg text="$parameter$"
This Parameter-as-variable method simply lets you pass that parameter as if it were a variable 
  •  eg text=<<__parameter__>> There are a FEW cases where this was essential. 

however I tend to find more value using the substitution forms in macros because there is less need to concatenate values
  • eg; tooltip="This is the $tooltipparam$  works"
  • And access a variable and use as substituton $(varname)$ (only in macro definitions)
The exception to this is demonstrated in this example where I imediatly convers parameters into variables to allow defaults to be computed.

\define macroname(input-parm)
<$set name=input-parm value="$input-parm$" emptyValue=<<othermacrofordefault>> >
   
Use input-parm=<<input-parm>> as a result I will not use the original $input-parm$ or the original <<__input-parm__>> in here
</$set>
\end
This method allows for more sophisticated or computed default values, and relies on emptyValue or in $list emptyMessage

So I do not mislead you another way to handle defaults for parameters that are literal strings is as follows
\define macroname(input-parm:"defaultstring")
Use the original $input-parm$ or the original <<__input-parm__>> in here
\end


Regards
Tones
To unsubscribe from this group and all its topics, send an email to tiddl...@googlegroups.com.

Eric Shulman

unread,
Sep 15, 2020, 9:03:51 PM9/15/20
to TiddlyWiki
On Tuesday, September 15, 2020 at 3:00:33 PM UTC-7, Dr Bob Jansen wrote:
One question. You reference the macro parameter using multiple braces but the documentation says to use <<__...__>>. Is this documented somewhere?

The "multiple braces" isn't the reference to the macro parameter.  It's a "filtered transclusion", which in this case enables use of the filter syntax to compute a value and then store it in a variable

Then, within that filter, I am using the $argname$ "placeholder" syntax, surrounded by "[...]" to insert the macro parameter value directly into the filter.
(see https://tiddlywiki.com/#Macro%20Definitions%20in%20WikiText, "Accessing variables and parameters")

Note that "<<__...__>>" is an alternative method of accessing a macro parameter ("parameter-as-variable") that is useful in places where a variable reference is normally used, while the "placeholder" syntax works by direct text substitution of the parameter's value into the surrounding syntax.  This substitution occurs before any of the macro's code is invoked, and thus can be used *anywhere* inside the macro, regardless of the context in which it occurs.

An example of the difference between these two methods would be concatenation of values.  For instance, using placeholders, we can join arguments together like this:
\define fullname(first,last) $first$ $last$
The result (the combined argument text) is then the "return" value of the macro, and can then be displayed as output or used as a widget parameter.

In contrast:
\define fullname(first,last) <<__first__>> <<__last__>>
Would not work, since the macro simply returns the content as shown, without any substitution, so it can't be used to display output, nor as a widget parameter because the <<__...__>> syntax is only recognized *within* a macro.

I very rarely make use of the <<__...__>> syntax, and when I *need* to make a variable from a macro parameter, I use an explicit $vars widget, like this:
<$vars varname="$param$>
The only time I use the "parameters-as-variables" syntax is to "help avoid issues with parameters that contain quotes", as described here:

Hope this helps...

-e

Dr. Bob Jansen

unread,
Sep 15, 2020, 9:43:54 PM9/15/20
to tiddl...@googlegroups.com
Thank you Eric and Tones. Very informative. I am learning, albeit slowly.

bobj

--
--------------------------------
Dr Bob Jansen
122 Cameron St, Rockdale NSW 2216, Australia
Ph (Korea): +82 10-4494-0328
Ph (Australia) +61 414 297 448
Resume: http://au.linkedin.com/in/bobjan
Skype: bobjtls
KakaoTalk: bobjtls
http://cultconv.com
Reply all
Reply to author
Forward
0 new messages