# Web technologies/2014-2015/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
### Velocity

Download from : http://apache.petsads.us//velocity/engine/1.7/velocity-1.7.zip (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.

### Jinja2 with Flask

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:
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. https://flask.palletsprojects.com/en/1.1.x/
Here's what the template.html file looks like:
In order to run the example one must have the following directory structure:
And to run Flask:
As similar to Velocity, Jinja2 also includes a set of directives:
* %if
* %else
* %elif
* %for
* %raw
* %extends
Links:
* Template Engine -- Wikipedia
* Business Logic -- Wikipedia
* Velocity -- Java templating engine
* Jinja2 -- Python Template Engine
* Flask -- Project Homepage
* 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 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