How to create a conditional argument in macros (or what am I missing)?

Twine Version: 2.9.2.0
Hi, so I am getting into using widgets as I realized that JS document.createElement is extremely tedious and widgets are kinda like react elements (for my web devs). But, for some reason, I can’t figure this one issue out, which I assume by the SugarCube documentations, is quite possible. For clarification, I have made other (even technically more complex) widgets that work fine, fetching a variable based on some value I gave it, so I am genuinely confused why this is the thing that is driving me crazy.

I have a very simple widget for creating a with some text that has a required class, and then an optional (arg) to add an additional class:

<<widget "altsystemtxt" container>>
	<<set _classes = _args[0] ? "textSystem " + _args[0] : "textSystem" >>
    <span @class="_classes"> _contents </span>
<</widget>>

And then I’d call it with (example):
<<altsystemtxt "error">>The World Happiness Percentage has fallen below the minimum value to keep this world functioning.<</altsystemtxt>>

I’ve logged it to the console and saw that _args was not undefined (nor _args[0]), and definitely existed. I have also tried setting the class directly to args[0] and brings up the same error:

Error: <<altsystemtxt>>: error within widget code ( The World Happiness Percentage has fallen below the minimum value to keep this world functioning. )

Though, to point out, the one time I didn’t have an error was when I didn’t separate “textSystem” and _args[0], so the class became “textSystemerror” and adding a space to make it "textSystem " caused the error too.

I honestly have no idea what this could mean. I also tried adding more quotes, changing to single, etc. based on the docs for widgets, but I still can’t figure it out.

So, what exactly am I missing?

Genuinely not sure if sugarcube supports ternary operators aside from just being on top of javascript. the documentation seems to indicate twinescript does not? If you want conditional logic, most people use the <<if>> macro.

https://www.motoslave.net/sugarcube/2/docs/#macros-macro-if

<<if [args[0]>>
<<set _classes = "textSystem " + _args[0]>>
<<else>>
<<set _classes = "textSystem">>
<</if>>

The issue isn’t the code in your widget definition, it’s the fact that you’re passing a String value of “error”.

Change that String to another value…

<<altsystemtxt "banana">>The World Happiness Percentage has fallen below the minimum value to keep this world functioning.<</altsystemtxt>>

… and the error message goes away.

It doesn’t seem to be about the argument precisely, it seems to be that output processing is different in a widget than when done directly…

If I have a widget with no arguments that tries to display a class="error" span:

<<widget "mywidget">><span class="error">This is some text.</span><</widget>>

Then <<mywidget>> displays as an error.

But <span class="error">This is some more text.</span> in the main body of a passage displays as simply a span with the error class.

Is this documented anywhere?

1 Like

Ok, I got it, that’s the problem. I don’t mind changing the values. I didn’t see it anywhere in the documentation that was in an obvious place, so just for future notice, was there a place where this was stated?

Sadly that was not the solution (it supposedly has to do with how “error” is handled), but, yes, ternary expressions are allowed even though I haven’t seen it in the documentation. I haven’t had as much luck with <<for>> statements though in pure JS.