r/twinegames • • 21d ago

SugarCube 2 use a variable as span id used in replace

Hi!

I'd like to use a variable as a span id, that will be targeted by a replace. It doesn't work so far. Is it possible?

This works (without variable):

<span id="oi">Oi!!</span>
<<link "clicky!">><<replace "#oi">>ow<</replace>><</link>>

This doesn't (exact same but with a variable) ("private names aren't valid in this context"):

<<set $ouch to "ouch">>
<span id=$ouch>ouch</span>
<<link "clicky!">><<replace `#$ouch`>>ow<</replace>><</link>>

This doesn't either (same too but a different way to set the variable) ("no element matched selector"):

<<set $heyWord to "hey">>
<<set $hey to "#" + $heyWord>>
<span id=$heyWord>hey!!</span>
<<link "clicky!">><<replace $hey>>ow<</replace>><</link>>

This works, so I assume the issue is with the span and not with the replace:

<<set $wowWord to "wow">>
<<set $wow to "#" + $wowWord>>
<span id="wow">Wowwww...</span>
<<link "clicky!">><<replace $wow>>ow<</replace>><</link>>

Is there a solution?

(I'm trying to generate this block with a widget, and pass the widget the value of the var that will be used as span id; I could just type it all out every time I need it - and will if there's no other solution - but I was looking for a more convenient way to do it)

3 Upvotes

6 comments sorted by

4

u/HiEv 21d ago

You can't just stick a variable into an HTML element, you need to mark it so that SugarCube will know it needs to be converted. You can do that by putting an "@" in front of the attribute name within the HTML element, which tells SugarCube to evaluate the value of that attribute before removing the "@". For example:

<<set $ouch to "owie">>
<span @id="$ouch">ouch</span>
<<link "clicky!">><<replace `"#" + $ouch`>>ow<</replace>><</link>>

The "@" causes the span to be rendered as <span id="owie"> in the above code.

The backticks within the <<replace>> cause it to evaluate the contents within the backticks, so it's effectively doing <<replace "#owie">> in the above code.

Enjoy! 🙂

2

u/nolanfa 20d ago edited 20d ago

Oooh, thanks! I had no clue about the @

(Edit: the paragraph here below is wrong, the code above works; see the following messages for details)

Weirdly, the backticks seem not to work in this specific case (error says "private names aren't valid in this context"), while concatenation in an outside var does (but concatenation in an outside var doesn't work with replace and several instances of the widget on the same page as the value of the outside var gets overwritten :'( I ended up just passing the span id as an argument... and having to remember the order of the arguments instead of having nicely-named vars >:'C ... but it works now and will be much easier if I need to change something) . But yeah, I picked up backtick use by hanging around here and they're very convenient.

2

u/HiEv 20d ago

The code I gave above works. I tested it before I posted it using SugarCube v2.37.3. I also just copy-pasted it and tried it again, and it still works, so Reddit didn't mangle it for me.

If you're getting an error from the backticks, it sounds like you did something wrong, likely something like forgetting to put the "#" inside of quotes within the backticks. Let me know exactly what you did, since I'm unable to reproduce that particular error message.

1

u/nolanfa 20d ago

Oh you're right, my bad. I had written it as `#$ouch` and I was so focused on the backticks I did not register any difference with your code.
This is so convenient! Thanks a lot!

1

u/GreyelfD 20d ago

The @ symbol usage mentioned by HiEv is known as Attribute Directive markup in the SugarCube documentation, and information about using back-quotes (aka backticks or grave-accent) to force the evaluation of an expression can be found in the Argument type macros: passing an expression as an argument section of the Macro Arguments documentation.

1

u/HiEv 20d ago

Very minor nitpick, but the "@" before an attribute in an HTML element actually called an "evaluation directive". "Attribute Directive" is the name of the section which describes it. That said, more people probably know it as the latter, and I've mistakenly called it that myself as well.