Snowman: Displaying passages within other passages, with if statements?

Twine Version: Snowman
Story Format: 2.3.5

Is there a way to do… the thing I’m trying to do with the code below? It doesn’t work. I’m sure it’s totally wrong! I’m trying to display different passages within the same passage depending on whether certain variables have been set.


<% if (story.examplevariable = "1" ) 
{
	%> story.passage("FirstPassage").render() <%
} 
else
{
	%> story.passage("SecondPassage").render() <%
} 
%>
1 Like

There are some issues (both potential and real) with your example:

  1. In JavaScript the single equals sign operator (=) represents assignment, where as the double and triple equals sign operators (== and ===) represent comparison.
    eg.
    assignment s.variable = "value"
    comparison if (s.variable === "value")

  2. In the template library being used by Snowman you use <% ..code.. %> markup to execute JavaScript statements that don’t generate output, and <%= ..code.. %> markup for those statements that do.

  3. [optional] You can use the s reference (instead of the story reference) within your JavaScript code to access the object used to store variables. eg. <% s.variable = "value" %>

  4. The function for generating the “rendered” HTML of a Passage is story.render(), as this is a JavaScript statement that generates output you need to place it within <%= ... %> markup.

Putting all the above together results in Passage code something like the following…

<% s.suburb = "here" %>

<% if (s.suburb === "here" ) { %>
	<%= story.render('FirstPassage') %>
<% } else { %>
	<%= story.render('SecondPassage') %>
<% } %>
1 Like

Thank you so much!!! That works! I knew I was doing everything wrong!