Two questions about templates

Twine Version: 2.3.13
Story Format: SugarCube 2.34.1
Browser: Mozilla Firefox 78.11.0esr (64 bits)

Good morning,

I’m exploring SugarCube tools, and I’ve tried to use a template.

First question: is there a way to store templates the same way we store widgets? I’ve tried to add a ‘templates’ tag to a passage but it didn’t achieve the hoped result. I’ve not been able to find anything about this with an internet search.

At there’s a quick description of some template functions, but it lacks a description of what is a template and how it works exactly in the first place.

Second question: what does mean the following sentence error? How can I fix it?
Error: <>: bad evaluation: cannot clobber existing template ?Couleur

As English is not my native language, I’m in trouble to understand the ‘clobber’ meaning. Thefreedictionnary gives:

tr.v. clob·bered , clob·ber·ing , clob·bers Slang

1. To strike violently and repeatedly; batter or maul.

2. To defeat decisively.

3. To criticize harshly.

Unfortunately none of the above meanings makes sense to me relatively to a coding feature.

As a matter of fact meaning is quite… meaningless if an internet search gives way to fix it. Alas I had no luck again.

So, to the basics: how did I hit this error?
Here’s the code.

<<script>>Template.add('Couleur', function(){return either("blanc","bleu","jaune","noir","rouge","vert");})<</script>>
Gaspard est habillé d'un T-shirt ?Couleur.

At this point it looks like it’s working well, one of the colour is randomly selected. If I refresh the page one is selected again, it may be the very same or another one, as intended.
The trouble comes with the ‘Go backwarkd within the game history’ button. Something happens or does not happen and the ‘Error message’ listed above appears. What is even more puzzling is that the function still selects a colour. So, basically, it’s an error message about something that otherwise looks to work well.

Could someone here please help me with one or both questions?

I’m not knowledgeable about Twine, but maybe this helps a bit nonetheless:

In a programming context, “to clobber” means to overwrite something (and usually the connotation is that it’s done without asking the user or without safeguarding against unintended consequences).

I think in this case it’s because you only need to add the template once, so (I guess) when the user goes back in the game history and then encounters that passage again, the program tries to add the template a second time, thus trying to overwrite the existing one.

The solution might be to add the template only at the start of the game, in some Setup object or passage, maybe StoryInit or something like that?

1 Like

You’re absolutely right. Templates can be stored in StoryInit, so it answers my first question. And your explanation addressed my second question. I had the feeling they might be somewhat linked.

Many thanks!

1 Like

You’re looking for the Template entry within the Markup section.

1 Like

What you point to was the reason I tried to use a template in the first place. What I was looking for is something really more consistent. Maybe it’s javascript docs, but I’m not sure either, as I don’t know javascript.

Templates really aren’t that complicated, the initial paragraph there covers what they are pretty well. The template markup (e.g., ?color) is replaced by the string yielded by the definition.