Best widget to hold a static variable?

I have a macro that does some calculations with tiddler widths etc and, as long as the widths are not changed, always gives the same output - say 350px

I also have a listwidget wherein the stuff uses the result from the above macro - in other words, it calls the macro for each interation in the listwidget.

The repeated calling+execution of the macro, to always get 350px, seems inefficient.

Can I call a static variable to get 350px instead of calling for it to be calculated every time?

I’m guessing $wikify or $set or our prettyboy $let but I’m not sure if any of them really holds a static string, of if its variable is recalculated every time it is invoked, and I don’t know how to test this.

Why not use a tiddler?

Well, the value should not be physically stored. It is just a variable.

I think \parameters is a semantic solution. But macro/procedure/$let/$vars should be fine.
I do not think there is a measurable performance effect using either of variable definition methods.
By the way, let’s see what advanced users think!

Of course there are state, temp and volatile tiddlers available for similar applications and you can create a function or variable based on such a tiddler or direct reference.

  • Such tiddlers can in effect only exist in memory
  • The thing is to set a field or tiddler needs a trigger so we need to consider if the change of the value has a trigger.
  • If I understand the tiddlywiki system changes to a tiddler will cascade through all its references

Alternatively perhaps CSS can be used.

The thing is to set a field or tiddler needs a trigger

Right; there is no trigger. My construct is a dynamic stylesheet, no “buttons” or whatever for the user to click and I think storing the result physically should not be needed.

To spare more suggestions about physically storing the value, here’s the actual thingy: It calculates the width of the tiddler content, which (as far as I know) has to be calculated:

<% if [{$:/themes/tiddlywiki/vanilla/options/sidebarlayout}prefix[fluid-fixed]] %>
calc(100vw - {{$:/themes/tiddlywiki/vanilla/metrics/sidebarwidth}} - 160px)
<% else %>
calc({{$:/themes/tiddlywiki/vanilla/metrics/tiddlerwidth}} - 110px)
<% endif %>

(I’m revising an old creation of mine so I am, at the moment, not quite sure where I get the magic numbers from but I guess it’s summed up tiddler gutters+margins.)

With every action in TW the stylesheets are recalculated. So I think there is no need for any triggers.

Right, but so are you saying that a variable inside the stylesheet, set via either of $wikify, $vars, $set, $let is necessarily always also recalculated (see OP)?

Which clues you in to the solution. You don’t want static, the value needs to be (re)calculated (but outside the loop, like we discussed recently but I can’t be arsed to go look up a link).

Can we see your <$list>…?


Aside: I tried to create a $set conditional variable assignment as a “$:/tags/Global” but I couldn’t get it to work:

<$set name=glob value="twMat"></$set>

<$set name=width-thing 
filter="[{$:/themes/tiddlywiki/vanilla/options/sidebarlayout}match[fluid-fixed]]" 
value="calc(100vw - {{$:/themes/tiddlywiki/vanilla/metrics/sidebarwidth}} - 160px)"
emptyValue="calc({{$:/themes/tiddlywiki/vanilla/metrics/tiddlerwidth}} - 110px)">
</$set>

<<glob>> renders fine, even in a style block, but <<width-thing>> isn’t rendered at all. I tested the filter in $:/AdvancedSearch (pass). I tested the value of <<width-thing>> inside the $set (pass). Seems conditional assignment does not work globally, it’s just “blank”. I tested a really simple one, glob2 in the following…

image

Bug, methinks. (@jeremyruston )

Those set widgets need to be nested, see the end of this docs tiddler https://tiddlywiki.com/#SetWidget

@saqimtiaz Didn’t really work. So I took the published example from the SetWidget doc and tried this:

Something wrong there.

I’ve never really liked to construct, tbh. Seems a bit screwy to me, (tramples over my understanding of “scope”). But the docs say it should work (and would be a nice/alternative solution (maybe) for Mat).

Neither this:

<$set name=r1 value=r1>
  <$set name=r2 value=r2>
    <$set name=r3 value=r3></$set>
  </$set>
</$set>

Nor this:

<$set name=r1 value=r1>
  <$set name=r2 value=r2></$set>
  <$set name=r3 value=r3></$set>
</$set>

works.

Thanks for input everyone!

I am as usual fiddling with a plugin for the community. Hope to release it today or else this weekend, so I’ll show everything in it to give the full context for the OP. Thanks again for looking at it!