Contents

Frontend Development › Rendering Strategies

Server-Side Templates

HTML generated by templates like Jinja, ERB or Thymeleaf.

Also known as: templating, template engine

A server-side template is an HTML file with placeholders. The server fills in the placeholders with data and sends the finished page to the browser. Jinja (Python), ERB (Ruby) and Thymeleaf (Java) are common examples.

<!-- templates/profile.html (Jinja) -->
<h1>Hello, {{ user.name }}</h1>
<ul>
  {% for order in orders %}
    <li>{{ order.title }}</li>
  {% endfor %}
</ul>
# The route fills in the template and returns HTML
return render_template("profile.html", user=user, orders=orders)

The browser receives complete HTML, so the content shows up without waiting for JavaScript. This is a good fit for multi-page apps, and it keeps logic on the server, where the data already is.

The classic mistake is inserting user input into HTML without escaping it, which allows cross-site scripting. Most template engines escape values by default, so don’t turn that off or build HTML by joining strings yourself. Also keep the templates focused on display. Put business rules in code you can test, not in the template.