You could also use cascade prefix. Only you need have stored your filters to call it. I read in documentatio the use with append, it is not simpler. I found a more simple version of cascade with append filter, but you have to store al filter together.
Yes, I mentioned that in the OP, and was trying that right now, getting stuck. I’ll post another version in a few minutes asking how to fix my approach.
I’ve been playing around with these. I think I agree with @stobot simply because it’s all too “wordy”. I did, however, have a brain-fart for a new operator…
I’m curious as to why the following alternative does not work. I’m almost certain it’s a simple syntax error, but I can’t find it. You can test it with this:
Offline I was exploring custom widgets and realised if you had a json array in a variable containing the value and icon pairs you could avoid the seperate data tiddler.
This prompted me to make a simple custom widget you give parameter value pairs to and it displays the result in a number of different formats including a json array. You can then copy it and set a variable to the array. Easier and safer than manualy writing it or having to create a data tiddler.
@Scott_Sauyet I will share my json data variable, using the above, later today. I am heading off on a bush walk.
I have a feeling I’m missing a key part of what you’re trying to solve, but this is where my mind was when I suggested it. A “responses” dictionary tiddler and then using the getindex[] call to grab it. I’m showing 3 different options here, one without functions using map to get the value into the getindex[], a function where you pass it the value to lookup - “won” field, and lastly a function baking in that it’ll always be the won field of the tiddler passed to it.
Sorry, I have become quite occupied on other essentials, so I will give a quick dump of what I was doing in this space, yet another branch to the original question. This Topic has generated a lot of interesting approaches.
Using the following widget we call it with an arbitary set of parameters and values, in this case the value and icon pairs.
This is a designer tool and need not exist in the published wiki
My first post had working code, and it’s what’s in my wiki now. I just felt it was full of too much duplication, which would simply get worse if I added more cases. I was hoping for a more streamlined approach. But it is mostly just an intellectual exercise. The solution I started with is not too problematic. I simply would like something better.
In more detail, the wiki is tracking information for a local political party. There was a recent election in my town, and I wanted to capture the results. Among the other data tiddlers are ones representing candidacies, with links to the person involved and the office for which this person is running. After the results were announced, I added to these tiddlers the votes received and the yes/no field for whether the candidate won.
I make reference to these data tiddlers in several different places: In the biographical tiddler for each candidate, in the descriptions of each of the offices being sought, and in an overview capturing the entire slate of candidates. In each of these places, I want to use the symbols to denote whether the candidate was successful. So I want this to be a reusable something: macro, function, procedure, widget.
I don’t need to map these. In any single usage, I want to pass a reference to a specific candidacy – using a variable such as cand or the generic currentTiddler – to this something and get back a the right icon.
Because this is captured in a single place, the extra duplication is not a major issue, but I thought someone would be able to point me to a better pattern for future case statements.
Yes, that was something I mentioned in the initial post, and tried to implement in a recent message. (Any suggestions for what’s wrong with that attempt would be much appreciated.) And @TW_Tones has a slightly different twist on the same idea as well.
Thank you; these are great! The first option won’t do for me, but the other two make sense. The third one is the same approach @etardiffdemonstrated above; it definitely helps reduce the duplication. The second one looks like a perhaps better way to do the same thing I tried and failed to do with a data tiddler.
I will try to expand a little on your second example, and report back.
? It works for my simple tests, but am I missing some edge cases?
Second, in this. which definitely would be my preferred way to call:
<<show.icon yes>>
I’m going to be limited to passing a value as the parameter, correct? I can’t pass a variable reference such as <currentTiddler> or some such? I would have to switch to macrocall/transclude to do so, right?
The point is that I want to pass the name of my tiddler to the function, and have the function lookup my won field to choose the symbol.
Agreed. Many thanks to you and to all the contributors!
I can give a full answer perhaps tomorrow but we can rewrite things to be less general and brief but also consider [show.icon{!!fieldname}] against the current tiddler. But if course we could rewrite things so <<show.icon fieldname>> works
if realised after my reply that show icon couldnow be something like candidate.status
you can always write an additional macro that invokes another with set parameters rather give the parameters every time.
In my case, ideally, I don’t start with the value, but with a tiddler that happens to have the value in a known field. Of course this can be done with the layering approach we’ve discussed elsewhere of having one function call another, passing some parameters supplied to it and other hard-coded ones.
I don’t think we have operators with an arbitrary number of parameters, but now with a procedure or a custom widget (and transclusions) and the $parameters widget, you can write a custom widget with an arbitrary number of parameters. If you do not name the parameters they are numbered.
\procedure testparameters()
<$parameters $params=all-params>
<<all-params>>
</$parameters>
\end
<<testparameters a b c d e f >>
Results in
{"0":"a","1":"b","2":"c","3":"d","4":"e","5":"f"}
You could then use a range operator to step through all parameters until is blank.
@Scott_Sauyet there has being a lot of water “under this bridge”, and many ideas suggested, but not all permutations thoroughly explored. I know I have gained a lot of inspiration and have myself run off on a number of tangents, both here and off line. It’s being very productive.
However I thought there was a few solutions to the Original Topic, implied in earlier code so I have now “lost the thread”. As a result I don’t think I can help (My bad) any further unless you “restate the question in full in a new reply” in light of what you have “learned”. It is getting hard to determine the meaning in relation to all the replies.
I call this from a few different templates across my wiki.
Again, this is working code. I do not need to fix it. But it feels far too redundant for what it does. I was looking for suggestions on how to fix it. There were many working suggestions. Two of the approaches suggested stand out. One, suggested by several people, but first by@etardiff, is this:
The other. which I hinted at the in the initial post, and which many people suggested, including @stobot and @TW_Tones is to store the configuration as data and use getindex to recover the specific value. I thought—and stobot’s suggestion was the same—that this would be stored in a data tiddler. TW_Tone’s pointed out that this could also be stored without the dedicated tiddler as a simple JSON string in a variable.
Those are the kinds of answers I was hoping for, and I learned a lot from them. But there’s one thing I haven’t been able to get right:
Open question
I’m still hoping that this can be further generalized to have a function that takes the name of the tiddler (or JSON variable) and the value to look up, for a general-purpose substitution configuration, and that we can then write specific functions that call this one, passing hard-coded references to a specific configuration.
I would hope to use the specific function like this:
both fail to return anything, even though the tiddler Candidacy/1 has a won property with the value no.
I would love to know what I’ve done wrong in that function.
An updated version of those tiddlers, which can be downloaded and dragged onto any 5.3.0+ wiki is here: UsingDataTiddler.json (6.1 KB)
Conclusion
I’ve learned a lot from this, and I hope others have as well. That was the goal, since I already had working code, and the duplication was more an annoyance than a serious flaw.
But I really hope someone might be able to tell me what’s wrong with my latter approach. I’ve tried all sorts of syntax, and feel like I’m reverting to magical thinking in my attempts. I’m guessing it’s something simple, but it could be deeper, and might reveal a big gap in my thinking.
Thank you
Thanks for everyone who has participated. I really have learned a lot.