# What IS Jinja

[Jinja](https://jinja.palletsprojects.com/en/2.11.x/) is a modern and designer-friendly templating language for Python, modeled after Django’s templates. It is fast, widely used, and secure with the optional sandboxed template execution environment. Jinja is basically an engine used to generate HTML or XML returned to the user via an HTTP response.

For those who have not been exposed to a templating language before, such languages essentially contain variables as well as some programming logic, which when evaluated (or rendered into HTML) are replaced with actual values.

### Why do we need [Jinja](https://jinja.palletsprojects.com/en/2.11.x/)?

**Sandboxed Execution** - It provides a protected framework for automation of testing programs, whose behavior is unknown and must be investigated.

**HTML Escaping** - Jinja has a powerful automatic HTML Escaping, which helps to prevent Cross-site Scripting (XSS Attack). There are special characters like >,<,&, etc. which carry special meanings in the templates. So, if you want to use them as regular text in your documents then, replace them with entities. Not doing so might lead to XSS-Attack.

**Template Inheritance** - This feature helps us to generate new pages starting from a base template that we inherit a common structure.

### [Jinja](https://jinja.palletsprojects.com/en/2.11.x/) Environment

Being a Python library, Jinja requires Python to run and expose the features. If you're not sure if Python is installed, just open a terminal and type `python --version`. The output should be something like this:

```bash
$ # Check Python version
$ python --version
Python 3.7.2 # <--- All good
```

**How to get Jinja2**

To start playing with it, just open a terminal and type:

```bash
$ pip install jinja2
```

**Jinja in action**

Simple runtime replace

```python
>>> from jinja2 import Template
>>> t = Template("Hello {{ token }}!")
>>> t.render(token="Jinja2")
u'Hello Jinja2!'
```

The engine will replace the inner token with value Jinja2. This is quite useful when we use this block for different token values.

**Lists iteration**

In web development, we can have cases when a list should be displayed on the page: registered users, for instance, or a simple list of options. In Jinja, we can use a `for structure` as bellow:

```python
# Define data structure
my_list=[0,1,2,3,4,5] # a simple list with integers
```

In Jinja, we can iterate with ease, using a for block:

```markup
...
      <ul>
        
{% for n in my_list %}
        <li>{{n}}</li>
        {% endfor %}

      </ul>
...
```

**Template Inheritance**

Templates usually take advantage of inheritance, which includes a single base template that defines the basic structure of all subsequent child templates. You use the tags { extends } and { block } to implement inheritance.

Let's take a look at a real sample:

**Parent HTML** - saved as base.html

```markup
<html>
  <head>
    <title>My Jinja 
{% block title %}{% endblock %} </title>
  </head>
  <body>
    <div class="container">
      <h2>This is from the base template</h2>
      <br>
      { block content }{ endblock }
      <br>
    </div>
  </body>
</html>
```

**The Child template** - saved as child.html

```markup
{ extends "base.html" }

{ block title } MySample { endblock }

{ block content }
  Cool content here
{ endblock }
```

When Jinja loads `child.html`, the `{ extends }` block informs the engine to merge the `base.html` template with the content provided by `child.html`.

* `{ block title }` becomes `MySample`
* `{ block content }` becomes `Cool content here`

Here is the final HTML generated by Jinja:

```markup
<html>
  <head>
    <title>My Jinja MySample</title>
  </head>
  <body>
    <div class="container">
      <h2>This is from the base template</h2>
      <br>
      Cool content here
      <br>
    </div>
  </body>
</html>
```

## Links & Resources

* [Jinja](https://jinja.palletsprojects.com/en/2.11.x/) - official website
* [Jinja Template](https://github.com/app-generator/jinja-template) - a curated list with starters published on Github


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://appseed.gitbook.io/docs/content/what-is/jinja.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
