use variable for macro param default

125 views
Skip to first unread message

RA

unread,
Mar 30, 2019, 11:59:26 PM3/30/19
to TiddlyWiki
Is it possible? I want to do something like this:

\define mymacro(c:variable_for_color)   <span style="color:variable_for_color"/>

So, there are 3 questions here really: defining a variable before defining a macro, using variable as a macro parameter default, and using variable inside html tag.

Mohammad

unread,
Mar 31, 2019, 1:12:41 AM3/31/19
to TiddlyWiki
Why noy use something like this

\define mymacro(c, text)   
<span style="color:$c$">
$text$
\end

Then use it as

<<mymacro blue "Hi, I am blue!">>

RA

unread,
Mar 31, 2019, 1:45:44 AM3/31/19
to TiddlyWiki
Yes, but I'd like `c` to have a default value, and I'd like to define that default not as a literal color value, but via a variable. Can you do that?

Mohammad

unread,
Mar 31, 2019, 5:04:40 AM3/31/19
to tiddl...@googlegroups.com
This is not an elegant solution and not semantic, but works!

\define spanmac(text) 
<span style="color:$(myColor)$;">
$text$
</span>
\end

\define mymacro(c:"", text)  
<$list filter="[enlist[$c$ $(vc)$]limit[1]]" variable=myColor>
<<spanmac """$text$""">>
</$list>
\end



<$vars vc="red" >

;Test one, no color sent
:<<mymacro text:"Hi, I am red!">>

;Test two
:<<mymacro c:blue text:"Hi, I am blue!">>
</$vars>


The vc has a default red color, so if you call the mymacro without c parameter, the default value here red is used!
If you pass c parameter, then it will be used! Try above code inside tiddlywiki.com

--Mohammad
Message has been deleted

S. S.

unread,
Mar 31, 2019, 7:19:19 AM3/31/19
to TiddlyWiki
RA,

See if this works for you:
\define myhelper(h) <span style="color:$h$"/>
\define mymacro(c:"variable_for_color") <$macrocall $name="myhelper" h=<<$c$>>/>

<$set name="variable_for_color" value="blue">
a <<mymacro>> b
</
$set>

Inspecting the underlying HTML gives:
<p>
a
<span style="color:blue"></span> b

</p>

Cheers

Mohammad

unread,
Mar 31, 2019, 8:28:16 AM3/31/19
to TiddlyWiki
Why this does not work?

<$set name="variable_for_color" value="blue">
a <<mymacro>> b

c <<mymacro red>> d
</$set>


It produces


<p> a <span style="color:blue"></span> b c <span style="color:"></span> d </p>

Mohammad

unread,
Mar 31, 2019, 8:31:41 AM3/31/19
to TiddlyWiki
Hi S.S,
 Your solution teaches one new rule
A variable can be referenced as default value in a macro definition by its name
So: c:<<varname>> or c:$(varname)$ does not work
BUT
c:"varname" works!

Is this is bug or it is a rule?

--Mohammad


On Sunday, March 31, 2019 at 3:49:19 PM UTC+4:30, S. S. wrote:

Mohammad

unread,
Mar 31, 2019, 8:34:05 AM3/31/19
to TiddlyWiki
Also this is interesting

h=<<$c$>>



On Sunday, March 31, 2019 at 3:49:19 PM UTC+4:30, S. S. wrote:

S. S.

unread,
Mar 31, 2019, 8:42:20 AM3/31/19
to tiddl...@googlegroups.com
Mohammad,

Since we are using the parameter c as a variable, we have to define its value as a variable first.
So if we wanted to pass the value red to the parameter c, we first have to define the variable red.
In this case we give the variable red the value red also.
Then it will work, as shown below.

\define myhelper(h) <span style="color:$h$"/>
\define mymacro(c:"variable_for_color") <$macrocall $name="myhelper" h=<<$c$>>/>

<$set name="variable_for_color" value="blue">
a <<mymacro>> b
</
$set>

<$set name="red" value="red">

c
<<mymacro red>> d
</$set>

Results in HTML produced:
<p>
a
<span style="color:blue"></span> b
</p><p>
c <span style="color:red"></
span> d
</p>

Regards.

Edited to include the missing opening <p> tag.

Mohammad

unread,
Mar 31, 2019, 8:52:49 AM3/31/19
to TiddlyWiki
Thanks for clarification.
It is interesting.

Best
Mohammad

TonyM

unread,
Mar 31, 2019, 6:09:54 PM3/31/19
to TiddlyWiki
Mohammad,

you and others are often referring to symantic, can you share a definition if possible. The term clashes in my mind with philosophy and natural languages use of the word.

Thanks
Tony

Mohammad

unread,
Apr 1, 2019, 12:39:57 AM4/1/19
to TiddlyWiki
Hi Tony,
and also Wikipewdia but in short!
Semantics is about the meaning of the sentence. It answers the questions: is this sentence valid? If so, what does the sentence mean?

For example I prefer the first piece of code below which create an aside


1.
<aside>
 
<h4>Epcot Center</h4>
 
<p>The Epcot Center is a theme park in Disney World, Florida.</p>
</aside>


2.
<div class="mycls">
 
<div style="font-size:2em">Epcot Center</div>
 
<p>The Epcot Center is a theme park in Disney World, Florida.</p>
</div>

Solution one is much more meaningful, easier to understand and easier to maintain, while both solutions are syntactically correct!


In linguistics, semantics is the study of meanings. In computer science, the term is frequently used to differentiate the meaning of an instruction from its format. The format, which covers the spelling of language components and the rules controlling how components are combined, is called the language's syntax. For example, if you misspell a command, it is a syntax error. If, on the other hand, you enter a legal command that does not make any sense in the current context, it is a semantic error.

--Mohammad

TonyM

unread,
Apr 1, 2019, 3:44:56 AM4/1/19
to TiddlyWiki
Mohammad,

This seems in fact to be like plane language and self documenting code. Written to be read, rather than simply demonstrate ones coding skills. Perhaps like basic vs c++

It seems to me with the broad audience tiddlywiki attracts, we should always code sharable material this way.

Thanks for that.
Tony

S. S.

unread,
Apr 1, 2019, 4:10:04 AM4/1/19
to TiddlyWiki
Mohammad,

In a similar vein, there is a new filter operator set for release in the next version 5.1.20: getvariable

Cheers!

Mohammad

unread,
Apr 1, 2019, 4:31:29 AM4/1/19
to TiddlyWiki
Yep, this gets the code more clear!

--Mohammad

TonyM

unread,
Apr 1, 2019, 6:40:08 AM4/1/19
to TiddlyWiki
S s

Also in my request to format variables in the view widget, Jeremy points out plans to introduce a format operator into filters. This will allow variables passed into a filter to return a value in the set widget filter or triple curly braces method after further transformation.

This is clearly indicating the further development of filters such that we can do more with variables including maths.

Regards
Tony

TonyM

unread,
Apr 4, 2019, 10:49:50 PM4/4/19
to TiddlyWiki
PS

As Posted elsewhere this is the method I use for default parameters

I often use a variation of this when making my own macros to allow it to use current tiddler or another if provided. Basically it provides a default of the current tiddler

However you could use this for any input parameter.

\define mymacro(tiddlername)
<$set name=selected-tiddler value="$tiddlername$" emptyValue=<<currentTiddler>> >
Action: <<selected-tiddler>>
</$set>
\end

<<mymacro>>
<<mymacro "another tiddler">>

This also illustrates another design principal, I almost always write my macros to operate on the current tiddler such that if placed in wikitext a list without a variable set the macros operate on the changing current tiddler, or it can be given a specific tiddler. If I use a variable in the list it uses the currentTiddler and the list will not change the currentTiddler value

Regards
Tony

Mohammad

unread,
Apr 5, 2019, 1:11:09 AM4/5/19
to TiddlyWiki
Added to TW-Scripts
Reply all
Reply to author
Forward
0 new messages