Jinja (template engine)

Last updated
Jinja
Original author(s) Armin Ronacher
Initial releaseJuly 17, 2008;16 years ago (2008-07-17) [1]
Stable release
3.1.4 [2]   OOjs UI icon edit-ltr-progressive.svg / 5 May 2024;5 months ago (5 May 2024)
Repository
Written in Python
Type Template engine
License BSD License
Website palletsprojects.com/p/jinja/   OOjs UI icon edit-ltr-progressive.svg

Jinja is a web template engine for the Python programming language. It was created by Armin Ronacher and is licensed under a BSD License. Jinja is similar to the Django template engine, but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox. It is a text-based template language and thus can be used to generate any markup as well as source code.

Contents

The Jinja template engine allows customization of tags, [3] filters (for formatting or transforming values [4] ), tests (for evaluating conditions [4] ), and globals. [5] Also, unlike the Django template engine, Jinja allows the template designer to call functions with arguments on objects. Jinja is Flask's default template engine [6] and it is also used by Ansible, [7] Trac, and Salt. [8] It is also used to make SQL macros, for example for use with dbt. [9]

Features

Some of the features of Jinja are: [10]

Jinja, like Smarty, also ships with an easy-to-use filter system similar to the Unix pipeline.

Syntax

The syntax for printing output in Jinja is using the double curly braces, for example {{ Hello, World! }}.

Statements which set variables in jinja or those which do not have an output can be wrapped within {% and %}, using the set keyword. For example {%setfoo=42%} sets a variable called foo with a value of 42.

Similar to above, comments in jinja can be written using a number sign (#) instead of a percentage (%), for example, {# helpful comment #}.

The syntax for creating a filter in Jinja is a vertical bar (|), for example {{ variable|filter }}. A variable can have multiple filters, for example {{ variable|filter|filter }}). [4]

The syntax for creating a test in Jinja is the keyword is as well as the conditions for evaluating the validity of a test, such as for example {%ifvariableisdivisibleby10%}do something{%endif%}). [4]

For loops can be used to iterate over sequences, while retaining their object properties. The following example demonstrates iterating over a list of users with username and password fields.

{%foruserinusers%}{{user.username}}{{user.password}}{%endfor%}

Although break and continue are not allowed inside loops, sequences can be filtered.

Example

Here is a small example of a template file example.html.jinja: [11]

<!DOCTYPE html><html><head><title>{{variable|escape}}</title></head><body>{%- foriteminitem_list%}{{item}}{%ifnotloop.last%},{%endif%}{%- endfor%}</body></html>

and templating code:

fromjinja2importTemplatewithopen('example.html.jinja')asf:tmpl=Template(f.read())print(tmpl.render(variable='Value with <unsafe> data',item_list=[1,2,3,4,5,6]))

This produces the HTML string:

<!DOCTYPE html><html><head><title>Value with &lt;unsafe&gt; data</title></head><body>     1,     2,     3,     4,     5,     6   </body></html>

Note the minus sign (-) after the tag {%: If you add a minus sign (-) to the start or end of a block (e.g. a For tag), a comment, or a variable expression, the whitespaces before or after that block will be removed. [12]

Related Research Articles

<span class="mw-page-title-main">HTML</span> HyperText Markup Language

Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScript.

Smarty is a web template system written in PHP. Smarty is primarily promoted as a tool for separation of concerns. Smarty is intended to simplify compartmentalization, allowing the front-end of a web page to change separately from its back-end. Ideally, this lowers costs and minimizes the efforts associated with software maintenance.

<span class="mw-page-title-main">Apache Tapestry</span> Open-source web application framework

Apache Tapestry is an open-source component-oriented Java web application framework conceptually similar to JavaServer Faces and Apache Wicket. Tapestry was created by Howard Lewis Ship, and was adopted by the Apache Software Foundation as a top-level project in 2006.

<span class="mw-page-title-main">Django (web framework)</span> Python web framework

Django is a free and open-source, Python-based web framework that runs on a web server. It follows the model–template–views (MTV) architectural pattern. It is maintained by the Django Software Foundation (DSF), an independent organization established in the US as a 501(c)(3) non-profit.

<span class="mw-page-title-main">TurboGears</span>

TurboGears is a Python web application framework consisting of several WSGI components such as WebOb, SQLAlchemy, Kajiki template language and Repoze.

A webform, web form or HTML form on a web page allows a user to enter data that is sent to a server for processing. Forms can resemble paper or database forms because web users fill out the forms using checkboxes, radio buttons, or text fields. For example, forms can be used to enter shipping or credit card data to order a product, or can be used to retrieve search results from a search engine.

HeaderDoc is a documentation generator developed and maintained by Apple Inc. Using specially commented source code files as input, HeaderDoc generates documentation for the code in HTML or XML format. Syntax for HeaderDoc comment tags is largely similar to, and as of HeaderDoc version 8, supportive of Javadoc tags. HeaderDoc 8.7 and later also provides partial support for many Doxygen tags. Apple's HeaderDoc project is free, open source software distributed under the Apple Public Source License.

In computer programming, boilerplate code, or simply boilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered verbose, the programmer must write a lot of boilerplate code to accomplish only minor functionality.

Roundup is an open-source issue or bug tracking system featuring a command-line, web and e-mail interface. It is written in Python and designed to be highly customizable.

Haml is a templating system that is designed to avoid writing inline code in a web document and make the HTML cleaner. Similar to other template systems like eRuby, Haml also embeds some code that gets executed during runtime and generates HTML code in order to provide some dynamic content. In order to run Haml code, files need to have a .haml extension. These files are similar to .erb or .eRuby files, which also help embed Ruby code while developing a web application.

The syntax and semantics of PHP, a programming language, form a set of rules that define how a PHP program can be written and interpreted.

Web2py is an open-source web application framework written in the Python programming language. Web2py allows web developers to program dynamic web content using Python. Web2py is designed to help reduce tedious web development tasks, such as developing web forms from scratch, although a web developer may build a form from scratch if required.

The Wing Python IDE is a family of integrated development environments (IDEs) from Wingware created specifically for the Python programming language with support for editing, testing, debugging, inspecting/browsing, and error-checking Python code.

<span class="mw-page-title-main">Flask (web framework)</span> Python web framework

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.

Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. Symfony PHP framework comes with a bundled support for Twig as its default template engine since version 2.

In database management systems (DBMS), a prepared statement, parameterized statement, or parameterized query is a feature where the database pre-compiles SQL code and stores the results, separating it from data. Benefits of prepared statements are:

Mustache is a web template system. It is described as a logic-less system because it lacks any explicit control flow statements, like if and else conditionals or for loops; however, both looping and conditional evaluation can be achieved using section tags processing lists and anonymous functions (lambdas). It is named "Mustache" because of heavy use of braces, { }, that resemble a sideways moustache. Mustache is used mainly for mobile and web applications.

Thymeleaf is a Java XML/XHTML/HTML5 template engine that can work both in web (servlet-based) and non-web environments. It is better suited for serving XHTML/HTML5 at the view layer of MVC-based web applications, but it can process any XML file even in offline environments. It provides full Spring Framework integration.

<span class="mw-page-title-main">Armin Ronacher</span> Austrian open source software programmer

Armin Ronacher is an Austrian open source software programmer and the creator of the Flask web framework for Python.

References

  1. "Jinja2 Release History" . Retrieved 24 June 2020.
  2. "Release 3.1.4". 5 May 2024. Retrieved 23 May 2024.
  3. "Extensions". Jinja2 Documentation (2.8-dev). Retrieved 2015-05-26.
  4. 1 2 3 4 "Jinja built-in filters and tests (like Django filters)". www.webforefront.com. Retrieved 2023-08-14.
  5. "Extensions". Jinja2 Documentation (2.8-dev). Retrieved 2015-05-26.
  6. DuPlain, R. (2013). Instant Flask Web Development. Packt Publishing. p. 30. ISBN   978-1-78216-963-5 . Retrieved 2015-05-26.
  7. "Templating (Jinja2) — Ansible Documentation".
  8. "Understanding Jinja". docs.saltproject.io.
  9. Jinja and macros | dbt Developer Hub
  10. "Welcome | Jinja2 (The Python Template Engine)". palletsprojects.com/p/jinja.
  11. Ronacher, Armin. "Template Designer Documentation". Jinja Documentation (3.0.x). Retrieved 9 January 2024. A Jinja template doesn't need to have a specific extension: .html, .xml, or any other extension is just fine.
  12. "Template Designer Documentation — Jinja Documentation (3.0.x)". jinja.palletsprojects.com. Retrieved 2024-01-09.