This is the 11th day of my participation in the August More Text Challenge. For details, see:August is more challenging

What is a template

At its simplest, a template is an HTML model sent from the Web back end to the forward end.

In the previous study, you’ve seen how requests and parameters are used in the FastApi. However, in our actual Web development, we will present in the form of web pages. Then in the FastApi we can also use a third party template engine to achieve the back end of the page template rendering.

Note: It is well known that most web projects today use the mainstream front-end separation model. But for the sake of learning, let’s start with back-end rendering.

FastApi template details

About the engine

FastApi commonly used Jinja2 template engine to achieve page rendering.

Although FastApi’s Starlette already encapsulates Jinja2 related class objects, you still need to install the Jinja2 engine to use it.

This place is weird

Ordinary rendering

The project structure

Using the engine

from fastapi import FastAPI

# Import Request context object, used to pass parameters between front and back
from starlette.requests import Request

# Import jinja2 template engine object for later use
from starlette.templating import Jinja2Templates

app=FastAPI()

# Instantiate a template engine object, specifying the path where the template resides
templates=Jinja2Templates(directory='templates')

@app.get('/index/{info}')

# Pass the request object in the view function to pass the context in the template object (also receive the path parameter info and pass it to the context)
async def index(request:Request,info:str) :
    # Return a template object and render the template using the data in the context
    return templates.TemplateResponse(name='index.html',context={'request':request,'info':info})

if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app='main:app',host='127.0.0.1',port=8765,reload=True)
Copy the code

Index.html content

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
    <title>FastApi</title>
</head>
<body>
    <h1>Index</h1>
    <h1>INFO:{{ info }}</h1>
</body>
</html>
Copy the code

Effects in the browser

http://127.0.0.1:8765/index/phyger

Above, we briefly showed how to render a template using the FastApi.

Render loop

View code

We put the looping rendered view into the FastApi Router.

@router.get('/indexs')

# Pass the request object in the view function to pass the context in the template object (also receive the path parameter info and pass it to the context)
async def index(request:Request) :
    # Define a list
    info = ["phyger"."python"."go"]
    # Return a template object and render the template using the data in the context
    return templates.TemplateResponse(name='index.html',context={'request':request,'info':info})
Copy the code

Page code

<body>
    <h1>Info</h1>
    <b>| the back of the reverse is in reverse order.</b>
    <ol>
        {% for msg in info|reverse  %}
            <li>{{ msg }}</li>
        {% else %}
            <li>It has no value</li>
        {% endfor %}
    </ol>
    <h1>{{ info }}</h1>
</body>
Copy the code

Effect of the page

Because we reversed the result of the loop in the Jinja template, the order of the list is reversed from the original list data.

Thank you for reading, and don’t forget to follow, like, comment, and forward four times!