Today we are taking a look at Python’s Template Engine called Jinja.
Many programmers today make use of templating techniques with languages like Python and PHP to …
- achieve things faster by placing programming logic into templates
- web pages generally run faster with templating techniques
- templating techniques aid with beautiful looking code (where looping statements stand out more)
So, today, with our Mac OS X local (MAMP) web server system, we show you a Python (2.7.10, for us) package manager pip install of Jinja (where we needed to have “sudo -H python -m ” prefix to command, but you may not) …
sudo -H python -m pip install markupsafe
sudo -H python -m pip install jinja2
… (ie. version 2 of Jinja).
Today, we are just introducing you to Jinja, so we started by looking up Wikipedia about it, and based some Python code around what was found there, but it ended up adding quite a bit of extra functionality parts to the top one of the list below …
- wrote the Python skeletal logic for creation of HTML showing a comma separated list of 6 items via a Template render() method … and then over the course of today we (also) …
- allowed for those 6 items to be defined from a command line call of this same Python code … and then thought …
- this is a “Three P’s Ride Again” scenario (if ever I saw a “Three P’s Ride Again” scenario) so let’s set up a “Three P’s Ride Again” scenario post haste, centring on some PHP which …
- can be called in our favourite 3 modes of use, namely Web Browsing, and at command line and via curl to …
- look for $_GET[] and/or $_POST[] calls (of itself) (representing the HTML form (method=POST action=callYourself) which it creates for itself at the end of its HTML (regardless)) and if existant arrange for the Python command line call (with argument list) be executed using the PHP exec method … and for …
- command line and curl modes of use, call on Perl (using its system() method) to call on (this same) Python to perform the (templating) work
So here are the Three Ps and their source code for this Jinja (Python) Template engine web application …
- Python test_jinja.py
- PHP test_jinja.php
- Perl test_jinja.pl
If this was interesting you may be interested in this too.
11 Responses to Jinja Template Engine Three Ps Primer Tutorial