0 votes
by (630 points)
edited by

 I'm trying to make a radiobutton use a variable as a part of the object propertyname that it sets but I can't figure out how to make it work in a sane way.

Usually I can use something like $object[$varforpropertynamepart1+$varforpropertynamepart2].something just fine but with radiobutton it doesn't seem to work.

:: Test1 [nobr]

<<if $characters is undefined>>
  <<set $charnames to ["Jack","Jill","Jean"]>>
  <<set $characters to {}>>
  <<for _i to 0;_i lt $charnames.length;_i++>>
  	<<set $characters[$charnames[_i]] to {}>>
  	<<set $characters[$charnames[_i]].gender to "unknown">>
  <</for>>
<</if>>

<<for _i to 0;_i lt $charnames.length;_i++>><br><br>
  Set <<print $charnames[_i]>>'s gender to
	<label>Female <<radiobutton "$characters[$charnames[_i]].gender" "female">></label>
	<label>Male <<radiobutton "$characters[$charnames[_i]].gender" "male">></label>/* doesn't process the name  */
  
  <br>For real try to set <<print $charnames[_i]>>'s gender to
	<<set $setgender to "$characters["+$charnames[_i]+"].gender">>/*  */
	<label>Female <<radiobutton $setgender "female">></label>
	<label>Male <<radiobutton $setgender "male">></label>
  <<if $charnames[_i] is "Jean">>
  <br>Set Jean's gender in horrible way that actually works to
    <label>Female <<radiobutton "$characters.Jean.gender" "female">></label>/* this shares the selection with above despite this actually working while it doesn't */
    <label>Male <<radiobutton "$characters.Jean.gender" "male">></label>
  <</if>>
<</for>>
<br><br>

[[Test2]]


:: Test2

<<print JSON.stringify($characters)>>

[[Test1]]

Here's what the relevant parts look like when inspecting the html, despite 2nd and 3rd looking the same only the 3rd actually works:

Set Jean's gender to
<label>Female <input id="radiobutton-characterscharnames-igender-4" name="radiobutton-characterscharnames-igender" type="radio" tabindex="0" class="macro-radiobutton"></label> 	

For real try to set Jean's gender to 
<label>Female <input id="radiobutton-charactersjeangender-0" name="radiobutton-charactersjeangender" type="radio" tabindex="0" class="macro-radiobutton"></label>

Set Jean's gender in horrible way that actually works to
<label>Female <input id="radiobutton-charactersjeangender-2" name="radiobutton-charactersjeangender" type="radio" tabindex="0" class="macro-radiobutton"></label>

 

1 Answer

+1 vote
by (44.7k points)
selected by
 
Best answer

You just need to use "backticks" (the ` on the ~ key) to evaluate the content of the variable parameter in a way that you won't pass more than one variable name.  To fix your above example you would do this:

	<label>Female <<radiobutton `"$characters." + $charnames[_i] + ".gender"` "female">></label>
	<label>Male <<radiobutton `"$characters." + $charnames[_i] + ".gender"` "male">></label>

In the above code, this part:

`"$characters." + $charnames[_i] + ".gender"`

when run will be evaluated as something like this:

"$characters.Jean.gender"

which is known as "dot notation" for object properties.

If you have any names with Unicode characters or symbols other than _ and $ in them, then you may want to use this format instead:

`"$characters['" + $charnames[_i] + "'].gender"`

which when run would be evaluated as something like this:

"$characters['Jean'].gender"

which is known as "bracket notation" for object properties.  "Dot notation" is generally shorter to type and easier to read, however "bracket notation" allows for more complicated property names and (normally) variables.

For more information see the "Passing an expression as an argument" section of the SugarCube documentation.

Hope that helps!  :-)

...