---
description: A template that shows an alternate layout during a fixed time window — two containers, a small lifecycle-safe script that checks the device clock every ten seconds, and the caveats about device time, gaps and the time-travel debug clock.
---

# Time-based layout override

This guide shows how to create a template that switches between a default layout (Base Template) and an alternate layout (Override Template) at specified times. You only need to declare the override time window. All other times show the base layout.

The pattern suits a sponsor loop over the coffee break, a "doors open" screen before the
first session, or a notice that must appear at a fixed time regardless of the schedule.
Both layouts live in one template, so nothing in the schedule has to change.

## The markup

Two full-screen containers inside the body, both using the base template's `.your-content`
styling, and the script last:

```html
<body>
  <div id="base-template" class="your-content">
    <h1 data-field="title"></h1>
    <p data-field="start_time_ampm end_time_ampm"></p>
  </div>

  <div id="override-template" class="your-content">
    <video src="./assets/video.mp4" autoplay loop muted playsinline></video>
    <p>Coffee break — back at 14:15</p>
  </div>

  <script src="script.js"></script>
</body>
```

```css
.your-content {
  /* the base template's rules, plus: */
  display: none; /* hidden until the script decides */
}
```

## The script

This script checks the local device time against a single override window. If the current time is inside the window, it shows the override template. Otherwise it shows the base template.

It uses the [template lifecycle hooks](template-js-best-practices.md), so the polling timer
is cleared automatically when the template switches out.

```js
window.preseriaTemplateInit = (ctx) => {
  // 1. The override window(s), in the device's local time.
  const overrideSchedule = [
    { from: new Date("2025-08-06T13:30:00"), to: new Date("2025-08-06T13:45:00") },
  ];

  // 2. Is the current time inside one of them?
  const isInOverrideSlot = (now) =>
    overrideSchedule.some(({ from, to }) => now >= from && now < to);

  // 3. Show one container, hide the other.
  function updateView() {
    const active = isInOverrideSlot(new Date());
    document.getElementById("base-template").style.display = active ? "none" : "block";
    document.getElementById("override-template").style.display = active ? "block" : "none";
  }

  // 4. Now, then every 10 seconds (auto-cleared on template switch).
  updateView();
  ctx.setInterval(updateView, 10 * 1000);
};
```

Add more windows to `overrideSchedule` for several takeovers a day; change the two
timestamps to move the window.

## What to watch

- This uses the device's local time. Ensure the device timezone is correct for your event.
- If the window falls in a gap, the override container must not rely on `data-field` values
  of a session: a gap carries only the room title, the time helpers and the gap flags (see
  [Template data fields](dynamic_fields.md)).
- Keep asset references in HTML and CSS (`./assets/…`); never build asset paths in
  JavaScript ([JavaScript rules](javascript_rules.md)).
- The `fakeNow` / `freezeTime` time-travel parameters move the player's internal clock, not
  `new Date()`, so they do not exercise this script — test the window with the real clock
  (see [JavaScript patterns and pitfalls](template_js_patterns.md#the-clock-and-time-travel)).

## Example template

[Download: Time-Based Layout Override](../../assets/templates/Time-Based%20Layout%20Override.zip)
— a session card and a coffee-break screen wired up through `preseriaTemplateInit(ctx)` and
`ctx.setInterval`. It binds the system fields `room`, `title`, `start_time_ampm` and
`end_time_ampm`, and two demo-only custom fields (`presenter_name`, `session_description`)
that you replace with your own JSON values or remove.

## Related

- [Template JavaScript best practices](template-js-best-practices.md)
- [Looping templates](looping_templates.md) — content that rotates on a fixed interval instead of at a fixed time
- [Template data fields](dynamic_fields.md)
