How do I pass a variable to a Javascript macro? (or don't I?)

148 views
Skip to first unread message

leeand00

unread,
May 22, 2020, 7:01:24 PM5/22/20
to TiddlyWiki
Hi,

I'm trying to pass a value to a variable to a javascript macro I'm trying to write; the result keeps coming back NaN, and I belive it is because I am passing "$(bgcol)$" instead of "7" for v:

Is there some other way I ought to be passing this argument?

<$set name=bgcol value="7">
<div style=<
<nowandthen m:11 v:2>>><<nowandthen m:11 v:"$(bgcol)$">>"$(bgcol)$"</div>
</$set>

/*\
title: $:/core/modules/macros/nowandthen.js
type: application/javascript
module-type: macro

Macro to return a formatted version of the current time

\*/

(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Information about this macro
*/


exports
.name = "nowandthen";

exports
.params = [
 
{name: "m"},
 
{name: "v"}
];

/*
Run the macro

m: is the highest value in the data set.
v: is the value to render.
*/

exports
.run = function(m, v) {

 let
out = "background-color: rgb(";

try {
 m
= parseFloat(m);
 v
= parseFloat(v);
 console
.log("m:" + typeof(m));
 console
.log("m:" + m);
 console
.log("v:" + typeof(v));
 console
.log("v:" + v);
 let huh
= v/m;
 let
where = huh*255;
 console
.log("huh: " + typeof(huh));
 console
.log("huh: " + huh);
 console
.log("where: " + typeof(where));
 console
.log("where: " + where);

} catch(e) {
 console
.log("error: " + e);
}

 
for(var i =0; i < 3; i++) {
 let wha
= ((parseFloat(v)/parseFloat(m)));
 
out = out + (Math.floor(wha*255)) + ", ";
 
}

 
out = (out.slice(0,-2)) + ");color:rgb(150,150,150);";

 
return out;
};

})();

Mark S.

unread,
May 22, 2020, 7:31:07 PM5/22/20
to TiddlyWiki
The substitution forms (e.g. $variable$, $(external variable)$ only work INSIDE a macro. So you have to
put

<div style=<<nowandthen m:11 v:2>>><<nowandthen m:11 v:"$(bgcol)$">>"$(bgcol)$"</div>

inside its own macro like:

\define misnamedmacro()
<div style=<<nowandthen m:11 v:2>>><<nowandthen m:11 v:"$(bgcol)$">>"$(bgcol)$"</div>
\end

and call it:


<$set name=bgcol value="7">
<<misnamedmacro>>
</$set>

Of course, there could be other things inside the JS macro that are wrong, but that's what's wrong out of the box.

Good luck!

leeand00

unread,
May 22, 2020, 8:27:07 PM5/22/20
to TiddlyWiki
Okay, so the $()$ syntax can only be used within a macro...gotcha;  Thanks!

Joshua Fontany

unread,
May 23, 2020, 7:37:01 PM5/23/20
to TiddlyWiki
In order to pass a value "by reference" to a macro call (while not inside a macro definition), you need to expand it out to the full <$macrocall> widget:
```
<$set name=bgcol value="7">
<div style=<
<nowandthen m:11 v:2>>>
  <$macrocall $name=nowandthen m=11 v=<<bgcol>> />"<<bgcol>>"
</div>
</$set>

```


Best,
Joshua Fontany
Reply all
Reply to author
Forward
0 new messages