Can you add uppercase in widget?

Twine Version: 2.10
Sugarcube 2.37

Hello, I want to create a widget, while most of it will be written in lowercase, sometimes I need it in uppercase.
Is there similar way $item.toLocaleUpperCase() but in widget?

Example case of usage (simplified version, since this is just a sample):

<<widget "item">>\
	<<if $item eq "knight">>\
		 sword
	<<elseif $item eq "mage">>\
		staff
	<</if>>\
<</widget>>\
You decide to pick your <<item>>. 
<<item>> are your favorite item

<<item>> are your favorite item → in this part you will need uppercase right? can you do this similar to $item.toLocaleUpperCase()? or you can’t?

Is there a reason this has to be a widget? I know you said this is a simplified example, but as it stands this looks like it would be easier to do with just variables.

I’m just curious if it is possible to add uppercase to widget or how much flexible widget is.

Since I don’t know much, I want to know the limit of using widget/when to use it/when I shouldn’t use it. (note: I knew widget is like creating template/macro code so far from my previous question).

It’s an interesting question, because to do it automatically would require the widget to know that it had been called at the start of a sentence, and as far as I know the widget has no access to the text surrounding it in the passage.

So to do what you are suggesting you’d either have to:

  1. Add another argument to the widget to tell it to make itself upper-case (e.g. <<item "sword" true>>
  2. Use a second widget to do the case-adjustment on its contents, e.g. (<<ufirst>><<item "sword">><</ufirst>>

If you look at @Chapel’s pronoun templates (not widgets, but the same idea) he’s had to make a ?He and a ?he to get around this, but widget names are not (I think) case sensitive, so you couldn’t make an <<item>> and an <<Item>> anyway.

1 Like

Something else you could do is use a Javascript function that returns a string, then use .toLocaleUpperFirst() on the string it returns. Put this in your Javascript section:

setup.item = function(){
  if (State.variables.item == "knight"){
  	return "sword";
  }
  else if (State.variables.item == "knight"){
    return "staff";
  }
}

Then you can use it like this:

<<print setup.item()>>
<<print setup.item().toLocaleUpperFirst()>>

Widget names are case sensitive. You can make both <<item>> and <<Item>>.

1 Like

Shows what I get for not checking first :smiley:

If the purpose of your <<item>> widget is to display a specific word based on the current value of a Story variable, then that outcome is better achieved using a Template, especially if there are times when you want the returned word to be proper-cased.

1: Define the collection of key & value pairs that will be needed.

The following code needs to be added to your project’s Story JavaScript area. It defines a property on the special setup object, to contain the list of ‘items’ and their associated ‘word’ values.

setup.items = { 'knight': 'sword', 'mage': 'staff' };

2: Define the ‘item’ template.

The following code also needs to be added to the project’s Story JavaScript area. It defines a Template named item that has an alias of Item. The later will be used to indicate if the ‘word’ outputted by the template should be proper-cased or not.

The State.variables getter is use to access the $item Story Variable from JavaScript, and the <String>.toUpperFirst() method is used to upper-case the first letter of the word being returned if needed.

Template.add(['item', 'Item'], function () {
	let word = setup.items[State.variables.item];
	return this.name === 'Item' ? word.toUpperFirst() : word;
});

3: Using the template

note that the first line is using the template’s name, because the all lower-case variant of the associated ‘word’ is needed. And the second line is using the template’s alias, because the proper-case variant of the associated ‘word’ is needed

You decide to pick your ?item. 
?Item are your favourite item
2 Likes

Thank you svlin, so that’s how to use javascript inside sugarcube :open_mouth:

I see, so widget doesn’t have that…
I guess widget is just there as shortcut for the code :open_mouth: , can’t manipulate it.
Thank you David

I see, so that’s when I need to use template :open_mouth: and use the javascript.

Thank you Greyelf for explaining it , your explaination is easy to understand.

It seems if I want more complex stuff in sugarcube I need to study javascript further.