- Flask Tutorial
- Flask Useful Resources
- Selected Reading
A template is a text document, or a normal Python string, that is marked-up using the Django template language. A template can contain block tags or variables. A block tag is a symbol within a template that does something. This definition is deliberately vague.
It is possible to return the output of a function bound to a certain URL in the form of HTML. For instance, in the following script, hello() function will render ‘Hello World’ with <h1> tag attached to it.
However, generating HTML content from Python code is cumbersome, especially when variable data and Python language elements like conditionals or loops need to be put. This would require frequent escaping from HTML.
This is where one can take advantage of Jinja2 template engine, on which Flask is based. Instead of returning hardcode HTML from the function, a HTML file can be rendered by the render_template() function.
Flask will try to find the HTML file in the templates folder, in the same folder in which this script is present.
- Application folder
- Hello.py
- templates
- hello.html
The term ‘web templating system’ refers to designing an HTML script in which the variable data can be inserted dynamically. A web template system comprises of a template engine, some kind of data source and a template processor.
Flask uses jinja2 template engine. A web template contains HTML syntax interspersed placeholders for variables and expressions (in these case Python expressions) which are replaced values when the template is rendered.
The following code is saved as hello.html in the templates folder.
Next, run the following script from Python shell.
As the development server starts running, open the browser and enter URL as − http://localhost:5000/hello/mvl
The variable part of URL is inserted at {{ name }} Friday edition of the journal science. place holder.
The jinja2 template engine uses the following delimiters for escaping from HTML.
- {% .. %} for Statements
- {{ .. }} for Expressions to print to the template output
- {# .. #} for Comments not included in the template output
- # .. ## for Line Statements
In the following example, use of conditional statement in the template is demonstrated. The URL rule to the hello() function accepts the integer parameter. It is passed to the hello.html template. Inside it, the value of number received (marks) is compared (greater or less than 50) and accordingly HTML is conditionally rendered.
The Python Script is as follows −
HTML template script of hello.html is as follows −
Note that the conditional statements if-else and endif are enclosed in delimiter {%.%}.
Run the Python script and visit URL http://localhost/hello/60 and then http://localhost/hello/30 to see the output of HTML changing conditionally.
The Python loop constructs can also be employed inside the template. In the following script, the result() function sends a dictionary object to template results.html when URL http://localhost:5000/result is opened in the browser.
The Template part of result.html employs a for loop to render key and value pairs of dictionary object result{} as cells of an HTML table.
Run the following code from Python shell.
Save the following HTML script as result.html in the templates folder.
Here, again the Python statements corresponding to the For loop are enclosed in {%.%} whereas, the expressions key and value are put inside {{ }}.
After the development starts running, open http://localhost:5000/result in the browser to get the following output.
The following table lists the various Web Template Engines used in Web template systems and a brief rundown of their features.
Engine (implementation)[a] | Languages[b] | License[c] | Variables[d] | Functions[e] | Includes[f] | Conditional inclusion[g] | Looping[h] | Evaluation (language)[i] | Assignment[j] | Errors and exceptions[k] | i18n[l] | Natural templates[m] | Inheritance[n] |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Apache Velocity | Java, C# | Apache | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ? | No |
ASP.NET (Microsoft) | C#, VB.NET | Proprietary | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ? | ? |
ASP.NET (Mono) | C# | LGPL | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ? | ? |
Laravel Blade | PHP | MIT | Yes | Yes | Yes | Yes | Yes | No | Yes | Yes | Yes | No | Yes |
Latte | PHP | BSD | Yes | Yes | Yes | Yes | Yes | No | Yes | Yes | Yes | No | Yes |
CheetahTemplate | Python | BSD | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ? | Yes |
CodeCharge Studio | Classic ASP, C#, VB.NET, PHP, Perl, Java/JSP, ColdFusion | Proprietary | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No | Yes | Yes | ? |
ColdFusion | CFML, CFScript | Proprietary | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
CTPP | C, C++, Perl, PHP, Python | BSD-like | Yes | Yes | Yes | Yes | Yes | No | No | Yes | ? | ? | ? |
Django | Python | BSD-like | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | Yes |
eRuby | Ruby | GNU GPL / Ruby License | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ? | |
FreeMarker | Java | Apache | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No | No |
Genshi (templating language) | Python | BSD-like | Yes | Yes | Yes | Yes | Yes | Yes (Python) | Yes | Yes | Yes | ? | Yes |
Go templates | Go | BSD | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No |
Haml | Ruby, PHP (WIP) | MIT | Yes | Yes | Yes | Yes | Yes | Yes (Ruby) | Yes | Yes | ? | ? | ? |
Hamlets | Java | BSD | Yes | Yes | Yes | Yes | Yes | No | Yes | Yes | ? | ? | ? |
JavaServer Pages | Java | CDDL + GNU GPL[1] | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ? | ? |
Jinja | Python | BSD | Yes | Yes | Yes | Yes | Yes | Yes (Python) | Yes | No | ? | ? | ? |
Jinja2 | Python | BSD | Yes | Yes | Yes | Yes | Yes | Yes (Python) | Yes | Yes | Yes | Yes | Yes |
Tera | Rust | MIT | Yes | Yes | Yes | Yes | Yes | No | Yes | Yes | ? | ? | ? |
Kid (templating language) | Python | MIT | Yes | Yes | Yes | Yes | Yes | Yes (Python) | Yes | Yes | ? | ? | ? |
Lucee | CFML | LGPL | Yes | Yes | Yes | Yes | Yes | Yes (CFML) | Yes | Yes | Yes | Yes | Yes |
Mustache | 30+ languages | MIT | Yes | Yes | Yes | Yes | Yes (foreach) | No | No | Yes | Yes | Yes | No |
Open Power Template | PHP 5 | BSD-like | Yes | Yes | Yes | Yes | Yes | Yes (PHP) | Yes | Yes | ? | ? | ? |
Pebble | Java | BSD-like | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
PHP | PHP | PHP License | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No |
Razor | C#, F#, VB.NET | Apache | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
Smarty | PHP | LGPL | Yes | Yes | Yes | Yes | Yes | Yes (PHP) | Yes | Yes? | Yes | ? | Yes |
Squirrelly | JS | MIT | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No | No |
Template Attribute Language | Various | open source | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ? |
Template Toolkit | Perl, Python | open source | Yes | Yes | Yes | Yes | Yes | Yes (Perl) | Yes | Yes | ? | ? | ? |
Thymeleaf | Java | Apache | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No |
TinyButStrong | PHP | LGPL | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? |
Twig | PHP | BSD | Yes | Yes | Yes | Yes | Yes | No | Yes | Yes | Yes | No | Yes |
vlibTemplate | PHP | Artistic License | Yes | Yes | Yes | Yes | Yes | No | No | No | ? | ? | ? |
web2py | Python | LGPL3 | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
WebMacro | Java | Apache, GNU GPL | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | ? | ? | ? |
Engine (implementation)[a] | Languages[b] | License[c] | Variables[d] | Functions[e] | Includes[f] | Conditional inclusion[g] | Looping[h] | Evaluation (language)[i] | Assignment[j] | Errors and exceptions[k] | i18n[l] | Natural templates[m] | Inheritance[n] |
See also[edit]
Notes[edit]
- ^ abEngine (implementation) : engine name
- ^ abLanguages : implementation language of the engine (not the template script language)
- ^ abLicense : Software license agreement
- ^ abVariables : script language power to use variables
- ^ abFunctions : script language power to use functions
- ^ abIncludes : script language power include external files
- ^ abConditional inclusion : script language power to conditional includes
- ^ abLooping : script language power to do loops (for, while) or recursion
- ^ abEvaluation (language) : script language power to do 'eval command' (to the implementation language)
- ^ abAssignment : set names and references to sub-templates (?)
- ^ abErrors and exceptions : engine output script errors.
- ^ abi18n : Internationalization and localization feature
- ^ abNatural templates : the template can be a document as valid as the final result, the engine syntax doesn't break the document's structure
- ^ abInheritance : Supports the ability to inherit a layout from a parent template, separately overriding arbitrary sections of the parent template's content.
References[edit]
Retrieved from 'https://en.wikipedia.org/w/index.php?title=Comparison_of_web_template_engines&oldid=937231367'