# Web Technologies/2021-2022/Laboratory 4
## Templating engines

A Templating Engine is software that is designed to process templates and content information to produce output documents. It can be successfully used in Web content generation. It allows separating
Examples include:
* Jinja (Python)
* Velocity (Java)
* Freemarker (Java)
* JavaServerPages (Java)
* Phptal (PHP)
* Smarty (PHP)
* ActiveServerPages (.NET)
* XSLT (XML)
The main advantages of Templating Engines is that they:
* Enforce the separation of functional responsibility (eg. designer from programmer)
* Promote division of labor
* Provide easier syntax
* Increase security
Templating Engines can be used in:
* Servlet-based Web applications
* Java and SQL code generation
* XML processing and transformation
* Text processing, such as RTF file generation

## Examples
### Jinja2 with the Flask framework

Jinja is the most widely used templating engine for Python. It is inspired from Django's templating language and it's also Flask's default engine. The project's homepage is https://jinja.palletsprojects.com/en/2.11.x/
Since jinja2 is already in the PyPi repositories, one can easily install it with Python's package manager, pip. The command installing Jinja2 directly is (NOTE: you can directly use pip install Flask, as the Flask package has a dependency for Jinja2 and it will automatically get installed):

#### Using a virtual environment (optional)

Since installing Python packages directly on your operating system is not recommended, using Python's virtual environments is usually a good idea. The principle here is to have a separate Python distribution and to install packages inside the separated environment. Creation and activation of a virtual environment works as such:
Or (in the case you are using Microsoft Windows):
Where X.X is the desired Python version the environment should contain, and <venv-name> is it's desired name. Once created and activated, one can check using the which python or which pip command to validate the fact the virtual environment's Python distribution is being used.

#### Flask app definition

We will use the minimal Flask framework, as by default Flask utilizes the Jinja templating engine.
Here's what the template.html file looks like:
In order to run the example one must have the following directory structure:
In order to run the web application with flask, the environment variable FLASK_APP has to point to index.py:
Or if running Microsoft Windows:
Then, starting the app:
Jinja2 also includes a set of directives:
* %if
* %else
* %elif
* %for
* %raw
* %extends

### Velocity

Download from : Apache Velocity - Download Page (Project website http://velocity.apache.org/download.cgi)
When using Velocity, information is usually retrieved from beans. Consider for example the following code which displays Welcome John to your first Velocity application. Your email address is john@student.com.
where the hello.vm file looks like:
IMPORTANT: notice that we do not have to use the exact getters for retrieving the value of the fields. Velocity takes care of that for use by using Java Reflection.
You can also use directives such as:
* #set
* #if
* #else
* #elseif
* #end
* #foreach
* #include
* #parse
* #macro
A complete reference for template syntax can be found here.
Links:
* Template Engine -- Wikipedia
* Business Logic -- Wikipedia
* Flask -- Project Homepage
* Jinja2 -- Python Template Engine
* Jinja template inheritance
* Velocity -- Java templating engine
* JavaWorld Velocity Tutorial

## Exercises

* Start from the data in XML format from the previous laboratory. Create beans and/or lists for it and populate them with data directly from within Java / Python code.
Generate the same HTML code, as in the previous laboratory, using a Velocity or Jinja template.
Gabriel Iuhasz, 2014-10-01, iuhasz.gabriel@e-uvt.ro
Alexandru Munteanu 2021-10-16, mailto:alexandru.munteanu@e-uvt.ro