GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.fullstackpython.com — Template Engines Template EnginesTemplate engines take in tokenized strings and produce rendered strings with values in place of the tokens as output. Templates are typically used as an intermediate format written by developers to programmatically produce one or more desired output formats, commonly HTML, XML or PDF. Why are template engines important?Template engines allow developers to generate desired content types, such as HTML, while using some of the data and programming constructs such as conditionals and for loops to manipulate the output. Template files that are created by developers and then processed by the template engine consist of prewritten markup and template tag blocks where data is inserted. For example, look at the first ten source lines of HTML of this webpage: <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="author" content="Matt Makai"> <meta name="description" content="Template engines provide programmatic output of formatted content such as HTML, XML or PDF."> <link rel="shortcut icon" href="//static.fullstackpython.com/fsp-fav.png"> Every one of the HTML lines above is standard for each page on Full Stack
Python, with the exception of the The
base.html Jinja template
used to generate Full Stack Python allows every page on the site to have
consistent HTML but dynamically generate the pieces that need to change
between pages when the static site generator
executes. The below code from the <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="author" content="Matt Makai"> {% block meta_header %}{% endblock %} <link rel="shortcut icon" href="//static.fullstackpython.com/fsp-fav.png"> In a typical WSGI application, the template engine would generate the HTML output response when an HTTP request comes in for a particular URL. Python template enginesThere are several popular Python template engines. A template engine implementation will fall somewhere on the spectrum between allowing arbitrary code execution and granting only a limited set of capabilities via template tags. A rough visual of the code in template spectrum can be seen below for four of the major Python template engines.
Jinja (Jinja2)Jinja, also known and referred to as "Jinja2", is a popular Python template engine written as a self-contained open source project. Some template engines, such as Django templates are provided as part of a larger web framework, which can make them difficult to reuse in projects outside their coupled library. Major Python open source applications such as the configuration management tools Ansible and SaltStack as well as the static site generator Pelican use the Jinja template engine by default for generating output files. There is a whole lot more to learn about Jinja on the Jinja2 page. Django templatingDjango comes with its own template engine in addition to supporting (as of Django 1.9) drop-in replacement with other template engines such as Jinja. Mako template engineMako was the default templating engine for the Pylons web framework and is one of many template engines supported by Pyramid. Mako has wide support as a replacement template engine for many other web frameworks as well. Other Python template engine implementationsThere are numerous Python template engine implementations that range from weekend hacks to actively developed mature libraries. These template engines are listed alphabetically:
Template engine implementation comparisonsThere are many Python template engine implementations in addition to the ones listed above. These resources can help you select a Python template engine implementation that works well for your project.
Template engine resourcesTemplate engines are often used with web frameworks a black box where input goes in, and rendered text magically appears out the other side. However, when something unexpected returns from a template engine it is useful to know how they work to aid your debugging. The following resources examine existing template engine design as well as how to build your own engine when that's necessary for your projects.
Do you want to learn about web frameworks, CSS or JavaScript next?
1. Introduction
2. Development Environments
3. Data
4. Web Development
Web Frameworks
Django
Flask
Bottle
Pyramid
TurboGears
Falcon
Morepath
Sanic
Other Web Frameworks
Template Engines
Jinja2
Mako
Django Templates
Web Design
HTML
Cascading Style Sheets (CSS)
Responsive Design
Minification
CSS Frameworks
Bootstrap
Foundation
JavaScript
React
Vue.js
Angular
Task Queues
Celery
Redis Queue (RQ)
Dramatiq
Static Site Generators
Pelican
Lektor
MkDocs
Testing
Unit Testing
Integration Testing
Debugging
Code Metrics
Networking
HTTPS
WebSockets
WebRTC
Web APIs
Microservices
Webhooks
Bots
API Creation
API Frameworks
Django REST Framework
API Integration
Twilio
Stripe
Slack
Okta
Security
SQL Injection
CSRF
5. Deployment
6. DevOps
Changelog
What Full Stack Means
About the Author
Future Directions
Page Statuses
...or view the full table of contents.
Full Stack Python
Full Stack Python
is an open book that explains concepts in plain language and provides
helpful resources for those topics.
Updates via Twitter & Facebook. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |