
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Advanced Concepts
- Python - Abstract Base Classes
- Python - Custom Exceptions
- Python - Higher Order Functions
- Python - Object Internals
- Python - Memory Management
- Python - Metaclasses
- Python - Metaprogramming with Metaclasses
- Python - Mocking and Stubbing
- Python - Monkey Patching
- Python - Signal Handling
- Python - Type Hints
- Python - Automation Tutorial
- Python - Humanize Package
- Python - Context Managers
- Python - Coroutines
- Python - Descriptors
- Python - Diagnosing and Fixing Memory Leaks
- Python - Immutable Data Structures
- Python Useful Resources
- Python - Questions & Answers
- Python - Interview Questions & Answers
- Python - Online Quiz
- Python - Quick Guide
- Python - Reference
- Python - Cheatsheet
- Python - Projects
- Python - Useful Resources
- Python - Discussion
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
Python - Templating
Templating in Python
Templating in Python is a technique used in web development to dynamically generate static HTML pages using templates and data.
In this tutorial, we will explore the basics of templating in Python, including installation, creating templates, and rendering templates with data, with a focus on the Jinja2 templating engine.
String Templates in Python
String templates in Python is a simple way to perform string substitutions. Python's string module includes the Template class, which provides an easy way to replace placeholders in a string with actual values.
The Template class in the string module is useful for dynamically forming a string object through a substitution technique described in PEP 292. Its simpler syntax and functionality make it easier to translate for internationalization purposes compared to other built-in string formatting facilities in Python.
Template strings use the $ symbol for substitution, immediately followed by an identifier that follows the rules of forming a valid Python identifier.
Creating a Template
To create a template, you instantiate the Template class with a string that contains placeholders prefixed with $ as shown below −
from string import Template template = Template("Hello, $name!")
Substituting Values
You can substitute values into the template using the substitute() method, which takes a dictionary of key-value pairs.
The substitute() method replaces the placeholders (identifiers) in the template with actual values. You can provide these values using keyword arguments or a dictionary. The method then returns a new string with the placeholders filled in.
Example: Using Keyword Arguments
Following code substitute identifiers in a template string using keyword arguments −
from string import Template tempStr = Template('Hello. My name is $name and my age is $age') newStr = tempStr.substitute(name = 'Pushpa', age = 26) print (newStr)
It will produce the following output −
Hello. My name is Pushpa and my age is 26
Example: Using a Dictionary
In the following example, we use a dictionary object to map the substitution identifiers in the template string −
from string import Template tempStr = Template('Hello. My name is $name and my age is $age') dct = {'name' : 'Pushpalata', 'age' : 25} newStr = tempStr.substitute(dct) print (newStr)
Following is the output of the above code −
Hello. My name is Pushpalata and my age is 25
Example: Missing Parameters Raises KeyError
If the substitute() method is not provided with sufficient parameters to be matched against the identifiers in the template string, Python raises KeyError −
from string import Template tempStr = Template('Hello. My name is $name and my age is $age') dct = {'name' : 'Pushpalata'} newStr = tempStr.substitute(dct) print (newStr)
Following is the error produced −
Traceback (most recent call last): File "/home/cg/root/667e441d9ebd5/main.py", line 5, in <module> newStr = tempStr.substitute(dct) File "/usr/lib/python3.10/string.py", line 121, in substitute return self.pattern.sub(convert, self.template) File "/usr/lib/python3.10/string.py", line 114, in convert return str(mapping[named]) KeyError: 'age'
Substituting Values Using safe_substitute() Method
The safe_substitute() method behaves similarly to substitute() method, except for the fact that it doesn't throw error if the keys are not sufficient or are not matching. Instead, the original placeholder will appear in the resulting string intact.
Example
In the following example, we are using the safe_substitue() method for substituting values −
from string import Template tempStr = Template('Hello. My name is $name and my age is $age') dct = {'name' : 'Pushpalata'} newStr = tempStr.safe_substitute(dct) print (newStr)
It will produce the following output −
Hello. My name is Pushpalata and my age is $age
Installing Jinja2
To use Jinja2 for templating in Python, you first need to install the library. Jinja2 is a powerful templating engine that is widely used in web development for rendering HTML. It can be installed easily using pip, Python's package installer −
pip install jinja2
Creating and Rendering Jinja2 Templates
Jinja2 is a powerful templating engine for Python that allows you to create dynamic content by blending static template files with data. This section explores how to create Jinja2 templates and render them with data.
Creating a Jinja2 Template
To create a Jinja2 template, you define a template string or load it from a file. Templates use double curly braces {{ ... }} for placeholders and support control structures like "loops" and "conditionals" with {% ... %}.
Example
Following is an example of a simple Jinja2 template stored in a file "template.html" −
<!DOCTYPE html> <html> <head> <title>Hello, {{ name }}!</title> </head> <body> <h1>Hello, {{ name }}!</h1> <p>Welcome to our site.</p> </body> </html>
Rendering a Jinja2 Template
To render a Jinja2 template, follow these steps −
Loading the Template − Load the template from a file or create it from a string.
Creating a Template Object − Use "jinja2.Template" to create a template object.
Rendering − Use the render() method on the template object, passing data as arguments or a dictionary.
Example
In here, we are rendering Jinja2 template −
from jinja2 import Template, FileSystemLoader, Environment # Loading a template from a file (template.html) file_loader = FileSystemLoader('.') env = Environment(loader=file_loader) template = env.get_template('template.html') # Rendering the template with data output = template.render(name='Alice') # Output the rendered template print(output)
The output of the rendered Jinja2 template would be an HTML document with the placeholders replaced by the actual data passed during rendering −
<!DOCTYPE html> <html> <head> <title>Hello, Alice!</title> </head> <body> <h1>Hello, Alice!</h1> <p>Welcome to our site.</p> </body> </html>
Advanced Jinja2 Features
Jinja2 supports various advanced features such as loops, conditionals, and custom filters, making it a powerful tool for creating complex templates.Template Inheritance
Jinja2 supports template inheritance, allowing you to create a base template with common elements (like headers, footers, navigation bars) and extend or override specific blocks in child templates. This promotes code reuse and maintainability in large projects.
Example
This HTML template file named "base.html" defines a basic structure for a web page using Jinja2 templating syntax.
It includes blocks "{% block title %}" and "{% block content %}" that can be overridden in derived templates to customize the title and main content of the page, respectively −
<!-- base.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}Default Title{% endblock %}</title> </head> <body> {% block content %}{% endblock %} </body> </html>
The following Jinja2 template file "child.html" extends the "base.html" template, overriding the title block to set it to "Child Page" and the content block to include an <h1> header with the text "Child Page Content".
<!-- child.html --> {% extends "base.html" %} {% block title %}Child Page{% endblock %} {% block content %} <h1>Child Page Content</h1> {% endblock %}
Loops
Jinja2 allows you to iterate over lists or other iterable objects using {% for %} loops. Following is an example of how you can use a loop to generate an unordered list (<ul>) in HTML −
<ul> {% for item in items %} <li>{{ item }}</li> {% endfor %} </ul>
Conditionals
Conditional statements in Jinja2 ({% if %} and {% else %}) is used to control the flow of your templates based on conditions. Here is an example where "Jinja2" checks if user exists and displays a personalized greeting if true; otherwise, it prompts to log in −
{% if user %} <p>Welcome, {{ user }}!</p> {% else %} <p>Please log in.</p> {% endif %}
Custom Filters
Custom filters in Jinja2 is used to define your own filters to manipulate data before displaying it in the template.
In the following example, a custom filter reverse is defined in Jinja2 to reverse the string "hello", resulting in "olleh" when applied in the template −
# Define a custom filter function def reverse_string(s): return s[::-1] # Register the filter with the Jinja2 environment env.filters['reverse'] = reverse_string
In your template, you can then apply the "reverse" filter to any string −
{{ "hello" | reverse }}
Following is the output obtained −
olleh