Harlowe - react to variable changes in body in header

Twine Version: 2.8.1.0

So here’s my problem: I want to have an (in-story) date shown in a title bar. I have defined a story passage tagged as “header”, which includes this snippet: <div id="date">$date</div> And in my passages, where appropriate, I do (set: $date to 'Wednesday, March 14th') or similar.

The problem is that the change from the (set:) macro only takes effect on the next passage, because the header has already been rendered when the (set:) is evaluated. That makes sense to me, but I do not know how I could work around it, being relatively new to Twine.

I could just move the (set:) to the preceding passage(s) but that seems annoying from an organizational standpoint - that doesn’t feel like the place where that data belongs, architecturally.

Maybe I could turn the header into a footer, then use CSS to pull it to the top of the page. Or are there any better alternatives - a macro that forces rerendering of the headers and footers, perhaps, or a macro that can react dynamically to variable changes?

That’s one way of doing it, but you can also use the (replace:) macro to update that element after the (set:) macro.

Like this

<div id="date">|date>[$date]</div>

(set: $date to "Wednestday")
(replace: ?date)[$date]

(the |date> inside the div is the target of ?date in the replace macro)
More about the (replace:) macro here

3 Likes

Thanks! I guess I’ll try to wrap that into a custom macro.

ETA: Arrived at this custom macro:

(set: $setDate to (macro: string-type _newDate, [
    (set: $date to _newDate)
    (replace: ?date)[$date]
    (output:)[]
]))

Along with <div id="date">|date>[$date]</div> in the header, invoked as ($setDate: 'Wednesday, March 14th').

Thanks again for your input!

1 Like