Replacing the same string with new text each time a choice is clicked

Twine Version: 2.3.8
Story Format: Harlowe 1.2.2

I’m trying to build a bookshelf where the player can examine multiple books, then have the book’s title appear below. Each time they click a new book, it should replace the existing title (if any), but I can’t figure out how to get this to work.

For example, in my Library passage:

(link-repeat: “Look at Book 1”)[(set: $book to ‘b001’)]
(link-repeat: “Look at Book 2”)[(set: $book to ‘b002’)]
(link-repeat: “Look at Book 3”)[(set: $book to ‘b003’)]

(display: “Books”)

Then in the passage titled “Books”:

{
(if: $book is ‘b001’)[
Lord of the Rings
](else-if: $book is ‘b002’)
Pride & Prejudice
[
]}

So if the player looks at Book 1, then I want them to be able to click Book 2 and have “The Lord of the Rings” replaced with “Pride and Prejudice”, except that I’m not seeing any text at all where the (display) macro is. It looks like the variable is setting, but not being called correctly. This is my first time using a display macro, so maybe I’m misunderstanding something. Any help appreciated!

Please use the Preformatted text option when including code examples, it doesn’t transform standard single & double quotes into invalid ‘curvy’ equivalents the same way that Blockquote does.

There are a number of ways to achieve the effect you want, although the different methods all use a similar technique of using a (replace:) macro to dynamically update the contents of a Named Hook. The following are two examples of using this technique…

Method 1: Adding the book-title to the contents of the (link-repeat:) macro’s associated hook.

(link-repeat: "Look at Book 1")[(set: $book to 'b001')(replace: ?title)[Lord of the Rings]]
(link-repeat: "Look at Book 2")[(set: $book to 'b002')(replace: ?title)[Pride & Prejudice]]
(link-repeat: "Look at Book 3")[(set: $book to 'b003')(replace: ?title)[Somthing else]]

|title>[]

Method 2: Using the contents of a ‘child’ Passage named Books.

2a. Contents of the ‘child’ passage.

{
(if: $book is 'b001')[Lord of the Rings]
(else-if: $book is 'b002')[Pride & Prejudice]
(else-if: $book is 'b003')[Something else]
}

2b. Contents of the ‘parent’ passage.

(link-repeat: "Look at Book 1")[(set: $book to 'b001')(replace: ?title)[(display: 'Books')]]
(link-repeat: "Look at Book 2")[(set: $book to 'b002')(replace: ?title)[(display: 'Books')]]
(link-repeat: "Look at Book 3")[(set: $book to 'b003')(replace: ?title)[(display: 'Books')]]

|title>[(display: 'Books')]