Using Jinja (Django) templates in Zato frontend services

  • Jinja is a templating language that Zato services can use to output HTML or other static resources typically needed by backend dashboards and other frontend applications.

  • For instance, a Zato service could accept input from frontend, connect to multiple backend systems to obtain the required data and then return a HTML with all the information ready for the frontend to display.

  • Note that Jinja is sufficiently similar to the templates that Django uses and if you know the latter, using Jinja will come naturally.

  • Zato already ships with Jinja and there is no need to install it separately.

Outputting HTML

Here is a sample frontend service that returns an HTML list representing clients. For simplicity, the actual information is returned directly from the "get_data" function but a real service could look it up in an external data store. Regardless of where the data comes from, the usage of Jinja is the same.

# -*- coding: utf-8 -*-

# Jinja
from jinja2 import Template

# Zato
from zato.server.service import Service

# ########################################################################################
# ########################################################################################

template = Template("""
    <ol>
        {% for item in items %}
            <li>{{ item }}</li>
        {% endfor %}
    </ol>
""")

# ########################################################################################
# ########################################################################################

class FrontedJinja(Service):

    name = 'api.frontend.jinja'

    def get_data(self):

        return [
            'Jane Doe',
            'John Brown',
            'Jackie Smith',
        ]

    def handle(self) -> 'None':

        # Obtain our data to produce the HTML for
        data = self.get_data()

        # A dictionary with configuration for the template
        config = {
            'items': data
        }

        # Generate HTML
        html = template.render(**config)

        # Assign HTML payload to the response
        self.response.payload = html

        # Set the output Content-Type as well
        self.response.headers['Content-Type'] = 'text/html'

# ########################################################################################
# ########################################################################################

The service will produce the following:

<ol>
    <li>Jane Doe</li>
    <li>John Brown</li>
    <li>Jackie Smith</li>
</ol>

Note that Jinja can be used to output other formats as well, e.g. CSV or any other information that will be easier to output with a templating language instead of constructing the responses manually.

Learn more


Schedule a meaningful demo

Book a demo with an expert who will help you build meaningful systems that match your ambitions

"For me, Zato Source is the only technology partner to help with operational improvements."

— John Adams
Program Manager of Channel Enablement at Keysight