---
url: /developers/frontend/template-rendering.md
---

# Template Rendering Guide

A deep dive into **server-side template rendering** — how Open Library transforms Python data into the HTML pages visitors see. This is the foundation layer of the frontend architecture described in the [Frontend overview](/developers/frontend/index.md).

> \[!NOTE]
> This guide covers Templetor, our legacy engine. New server-side templates use [Jinja](https://jinja.palletsprojects.com/) instead, and we convert old templates over time. See the [Jinja Templates Guide](/developers/frontend/jinja.md).

## The Anatomy of the Site

### HTML Templates

When a patron navigates the Open Library website and requests pages (like the screenshot pictured below), the patron is served "rendered" HTML content from the [`openlibrary/templates`](https://github.com/internetarchive/openlibrary/tree/master/openlibrary/templates) directory.

Here are a few examples of common templates:

* Homepage: [`home/index.html`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/home/index.html), which gets rendered in [home.py](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/plugins/openlibrary/home.py)
* Books Page(s): [`type/edition/view.html`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/type/edition/view.html) for both editions (e.g. <https://openlibrary.org/books/OL30162974M>) or works (<https://openlibrary.org/works/OL257943W>),
* Subjects Page(s): [`subjects.html`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/subjects.html) (e.g. <https://openlibrary.org/subjects/climbing>)
* Search: [`work_search.html`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/work_search.html) (e.g. <https://openlibrary.org/search>)
* Login & Register: [`login.html`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/login.html) and [`account/create.html`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/account/create.html)

> \[!NOTE]\
> Some pages, like the /books page, are first-class "registered" [Infogami](https://openlibrary.org/dev/docs/infogami) [`types`](https://github.com/internetarchive/openlibrary/tree/master/openlibrary/templates/type) and are powered through the Infogami wiki framework that runs Open Library. This is true for `books`, `authors`, `lists`, and a few other types. Because these are first-class registered types handled by infogami, You won't find [plugin](https://github.com/internetarchive/openlibrary/tree/master/openlibrary/plugins) controllers for them in Open Library. Their html templates can be found in the special [`templates/type`](https://github.com/internetarchive/openlibrary/tree/master/openlibrary/templates/type) directory. When Open Library notices a url containing a key for one of these special types, infogami fetches the corresponding object from the database and passes it as `page` straight into the template. So for the books page, which is [`templates/type/edition/view.html`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/type/edition/view.html), the `page` value defined in its [`$def with (page, ...)`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/type/edition/view.html#L1) header is actually a [`book` edition or work object](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/plugins/upstream/models.py#L44) whose properties are defined in [models.py](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/plugins/upstream/models.py#L44).

Most HTML pages within the templates folder do not include the `<html>`, `<head>`, and `<footer>` sections of the website—only the body content. Open Library automatically wraps nearly all `page` templates in the site layout, [`shell/base.html.jinja`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/shell/base.html.jinja).

### The Site Wrapper

While the body of each Open Library page may present differently, most pages share similar scaffolding, look, and feel. For instance, nearly every page will have:

1. A top black bar ([`shell/alert.html.jinja`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/shell/alert.html.jinja)) with an Internet Archive logo:
2. A header ([`shell/header.html.jinja`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/shell/header.html.jinja)) section with the Open Library logo, a search box, a hamburger menu, and an account dropper for logged in patrons:
3. A footer ([`shell/footer.html.jinja`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/shell/footer.html.jinja)) menu:

The layout that defines the overall structure of every Open Library page is the Jinja template [`templates/shell/base.html.jinja`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/shell/base.html.jinja). It includes smaller templates from the [`templates/shell/`](https://github.com/internetarchive/openlibrary/tree/master/openlibrary/templates/shell) directory, such as the [3 described above](#site-subtemplate-examples), and the Python in [`openlibrary/core/shell/`](https://github.com/internetarchive/openlibrary/tree/master/openlibrary/core/shell) prepares the data they need. The same layout is used for pages served by web.py and by FastAPI.

For web.py pages, the layout is registered under the name `site` (`SiteLayoutTemplate` in [`core/shell/context.py`](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/core/shell/context.py)), following web.py's ["layout template"](https://webpy.org/cookbook/layout_template) idea. The part of the layout that changes with each request is the `page`: the rendered page template, which `base.html.jinja` places inside `<main>`. You may wish to review examples in the [Page Templates](#html-templates) section.

### How a Page Gets Rendered

To serve a rendered template to a visitor, use the `render_template()` function (see its use in [home.py](https://github.com/internetarchive/openlibrary/blob/master/openlibrary/plugins/openlibrary/home.py)). The first argument is the name of the page-specific HTML template to render and inject into the `site` body. The remaining parameters pass variables from Python into the corresponding HTML template file, as defined in the template's header.

For instance, imagine we have a simple template called `templates/book.html` which requires a `book_title`, a `bookcover_url`, and an `author_name`. Such a template might look like:

```py
$def with (book_title, bookcover_url, author=None)

<div>
  <h1>$book_title</h1>
  <img src="$bookcover_url"/>
  $if author:
    <h2>$author</h2>
</div>
```

The `$def with(...)` line is how the template declares what variables it needs to be passed in when it is being called and rendered. Elsewhere in the template, the `$` symbol acts as an instruction that a variable should be replaced by its value when it's being rendered.

Check the [official web.py templator documentation](https://webpy.org/docs/0.3/templetor) if you'd like a deep dive into all the capabilities of Open Library's python-powered templating system.

> \[!CAUTION]
> The `$:` combination instructs the template to render this variable as html, as opposed to plaintext. This is sometimes necessary when rendering one html template from another, but should be done with care when rendering a variable whose value may be user-specified because this may result in dangerous [XSS attacks](https://en.wikipedia.org/wiki/Cross-site_scripting), where a bad actor may intentionally save javascript code into a variable, hoping that the website will render and execute this code as html as opposed to safe plaintext.

> \[!NOTE]\
> Notice that `author` in this example is provided as a keyword argument with a default value of `None` and thus is optional when the template is being called. Similar to when defining a python function, once a keyword argument is defined, all following variables after it must also then be keyword arguments.

From python, the corresponding code to render this `templates/books.html` template would be:

```py
render_template(
  'books',  # name of the template in templates/ directory without .html
  book_title="The Hobbit",
  bookcover_url="https://covers.openlibrary.org/b/id/14624642-L.jpg",
  author="J.R.R. Tolkien"
)
```

> \[!TIP]\
> When we call `render_template`, it knows to look in the `templates/` directory and so we don't need to specify this. Also, we don't need to include the `.html` extension.

When we call `render_template('book', ...)` we are fetching the `templates/book.html` template and passing in the values `book_title`, `bookcover_url`, and `author` from python into the template's `$def with (...)` section, where it can be substituted as needed into the `$variables` in the template. The rendered `book` template is then passed to the site layout, which places it inside the page's `<main>` section.

## Managing Large Templates

When a template becomes too large, or when common logic can be factored out, templates can be broken into smaller logical components and moved into separate micro-templates. These micro-templates can live in the `templates/` directory. If a micro-template is self-contained and functions as a standalone widget (such as `QueryCarousel`), consider saving it in the `macros` directory—a folder of templates with additional special properties.

## [Macros](https://openlibrary.org/dev/docs/infogami#anchor7)

When refactoring, you may decide to move code to the `macros` directory. A `macro` is a special type of `template` that can be accessed using `{{}}` syntax by the page editor in Open Library. For more information about macros, see the very stale/outdated [infogami documentation](https://openlibrary.org/dev/docs/infogami#anchor7).

Here's what the infogami edit UI looks like for a collection:&#xA;

Here's what the page looks like when it's rendered with a macro:&#xA;

New pages are added as FastAPI routes in `openlibrary/fastapi/`, not as web.py handlers. See the [FastAPI pages guide](https://github.com/internetarchive/openlibrary/blob/master/docs/core/fastapi-pages.md) in the codebase. The [Plugins Guide](https://github.com/internetarchive/openlibrary/tree/master/openlibrary/plugins#readme) explains the older web.py handlers you will still find in `openlibrary/plugins/`.
