Repository navigation
template rendering
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.
Note
This guide covers Templetor, our legacy engine. New server-side templates use Jinja instead, and we convert old templates over time. See the Jinja Templates Guide.
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 directory.
Here are a few examples of common templates:
- Homepage:
home/index.html, which gets rendered in home.py - Books Page(s):
type/edition/view.htmlfor both editions (e.g. https://openlibrary.org/books/OL30162974M) or works (https://openlibrary.org/works/OL257943W), - Subjects Page(s):
subjects.html(e.g. https://openlibrary.org/subjects/climbing) - Search:
work_search.html(e.g. https://openlibrary.org/search) - Login & Register:
login.htmlandaccount/create.html
Note
Some pages, like the /books page, are first-class "registered" Infogami types 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 controllers for them in Open Library. Their html templates can be found in the special 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, the page value defined in its $def with (page, ...) header is actually a book edition or work object whose properties are defined in models.py.
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.
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:
- A top black bar (
shell/alert.html.jinja) with an Internet Archive logo:
- A header (
shell/header.html.jinja) section with the Open Library logo, a search box, a hamburger menu, and an account dropper for logged in patrons:
- A footer (
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. It includes smaller templates from the templates/shell/ directory, such as the 3 described above, and the Python in 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), following web.py's "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 section.
To serve a rendered template to a visitor, use the render_template() function (see its use in 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:
$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 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, 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:
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.
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.
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.
Here's what the infogami edit UI looks like for a collection:

Here's what the page looks like when it's rendered with a macro:

New pages are added as FastAPI routes in openlibrary/fastapi/, not as web.py handlers. See the FastAPI pages guide in the codebase. The Plugins Guide explains the older web.py handlers you will still find in openlibrary/plugins/.