Python Pyramid – Templates

Templates are a crucial part of any web application, enabling developers to create dynamic HTML content by embedding Python code within HTML. Pyramid supports several templating engines, including Jinja2, Chameleon, and Mako. Each of these engines has its own syntax and features, but they all serve the same basic purpose: to generate HTML content dynamically.

Concepts Related to Python Pyramid Templates

  • Template Engines: Jinja2, Chameleon, and Mako are some of the templating engines that Python Pyramid supports. With these template engines, developers can write HTML pages dynamically by inserting Python code into HTML templates.
  • Inheritance of Templates: It is possible to create a base template with common elements like headers, footers, or navigation bars in it and then extend this base template in child templates to add specific content; this feature is called template inheritance. It helps in reusing code and having consistent layouts across multiple pages.
  • Template Context: The term “template context” refers to information passed from views (in Python) to templates. In Python Pyramid, you can pass data to templates either via the render() function or through context argument while rendering a template.

Setting Up Templates in Pyramid

To get started with templating in Pyramid, you need to choose a templating engine. In this article, we will focus on Jinja2, one of the most popular and widely used templating engines.

Using Jinja2 Template Engine

Creating a small project using Jinja Template with project file structure as given below



Step 1: Create a Pyramid project of Jina2

Step2 : Now Create a Jinja2 Template

Create a new Jinja2 template file with the .jinja2 extension. For example, I’m creating hello_world.jinja2 in the project’s templates directory.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello, {{ name }}!</title>
</head>
<body>
    <h1>Hello, {{ name }}!</h1>
    <p>Welcome to {{ city }}, in {{ country }}
</body>
</html>

Step3 : Write Pyramid View Function

Now, create a view function in default.py in views folder.

Python
@view_config(route_name='another')
def hello_world(request):
    context = {'name': 'Johny','city':'Lucknow','country':'India'}
    return render_to_response('myproject:templates/hello_world.jinja2',
                              context, request=request)

Step4: Register route

Register the view function in the routes.py file.

config.add_route('another', '/another')

Step 5 : To run the development server follow this command.

 pserve development.ini

The project runs successfully to see that open the serving URL(http://127.0.0.1:6543) and open it on the browser.

Complete Code

Python
# myproject:routes.py

def includeme(config):
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.add_route('another', '/another')
Python
# myproject:views/default.py
from pyramid.view import view_config
from pyramid.renderers import render_to_response

@view_config(route_name='home', renderer='myproject:templates/mytemplate.jinja2')
def my_view(request):
    return {'project': 'myproject'}

@view_config(route_name='another')
def hello_world(request):
    context = {'name': 'Johny','city':'Lucknow','country':'India'}
    return render_to_response('myproject:templates/hello_world.jinja2', context, request=request)
HTML
#myproject:templates/mytemplate.jinja2
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello, {{ name }}!</title>
</head>
<body>
    <h1>Hello, {{ name }}!</h1>
    <p>Welcome to {{ city }}, in {{ country }}
</body>
</html>

Output:

Using Chameleon Template Engine

Creating a small project using Chameleon Template with project file structure as given below



Step1: Create a Pyramid project of Chameleon

Step 2: Now Create a Chameleon Template

Create a new Chameleon template file with the .pt extension. For example, I’m creating base.pt and home.pt in the project’s templates directory.

base.pt

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Wesbite</title>
</head>
<body>
    <header>
        <h1>Welcome to my website...!</h1>
    </header>
    <main>${content}</main>
    <footer>© 2024 My Website</footer>
</body>
</html>

home.pt

HTML
<div>Heyy ${name}! Welcome to my Home Page!</div>

Step3: Write Pyramid View Function

Now, create a view function in default.py in views folder.

Python
@view_config(route_name='another')
def home(request):
    context = {'name': 'Rony'}
    return render_to_response('project2:templates/home.pt', context, request=request)

Step4: Register route

register the view function in the routes.py file.

config.add_route('another', '/another')

Step 5 : To run the development server follow this command.

>> pserve development.ini

The project runs successfully to see that open the serving URL (http://127.0.0.1:6543) and open it on the browser.

Complete Code

Python
#project2:views/default.py
from pyramid.view import view_config
from pyramid.renderers import render_to_response

@view_config(route_name='home', renderer='project2:templates/mytemplate.pt')
def my_view(request):
    return {'project': 'project2'}

@view_config(route_name='another')
def home(request):
    context = {'name': 'Rony'}
    return render_to_response('project2:templates/home.pt', context, request=request)
Python
#project2:routes.py
def includeme(config):
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.add_route('another', '/another')
HTML
#project2:templates/base.pt
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My Wesbite</title>
</head>
<body>
    <header>
        <h1>Welcome to my website...!</h1>
    </header>
    <main>${content}</main>
    <footer>© 2024 My Website</footer>
</body>
</html>
HTML
#project2:templates/home.pt
<div>Heyy ${name}! Welcome to my Home Page!</div>

Output:




Contact Us