Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Jinja - Short introduction and Sample Apps
Sm0ke
Sm0ke

Posted on • Edited on

     

Jinja - Short introduction and Sample Apps

Hello Coders,

This article is ashort introduction to Jinja, a modern templating language used by Python programmers in frameworks like Flask, Bottle, optionally in Django (starting with 1.8 version). For those already familiar with Jinja2, and fast-runners, I'll drop some links below to some nice Jinja Templates, provided as super simple Flask starters (no database or hard dependencies):

Thanks for reading! - Content provided byApp Generator.



Flask/Jinja Pixel UI

Pixel is a free, fully responsive and modern Bootstrap 4 UI Kit that will help you build creative and professional websites. Use our components and sections, switch some Sass variables to build and arrange pages to best suit your needs. Pixel Lite comes with 6 premium example pages that we created to show you the beautiful user interfaces that can be created.



Jinja Pixel Lite - Template project provided by AppSeed.


Flask/Jinja Datta Able

Datta Able comes with error/bug-free, well structured, well-commented code and regularly with all latest updated code. Which saves your a large amount of developing backend application time and it is fully customizable. This modern UI Kit crafted by CodedThemes features a rich UI Kit and pre-built pages: dashboard, maps and authentication pages.



Jinja Template - Datta Able, thumbnail image.


Flask/Jinja Bootstrap 5 Volt

Volt Dashboard is a free and open source Bootstrap 5 Admin Dashboard featuring over 100 components, 11 example pages and 3 plugins with Vanilla JS. There are more than 100 free Bootstrap 5 components included some of them being buttons, alerts, modals, datepickers and so on.


Jinja Volt Dashboard - Starter provided by AppSeed.


Flask/Jinja Material Lite Wpx

WrapPixel's MaterialPro Lite is one of the best Bootstrap templates for admin dashboards and control admin panels. It combines colors that are easy on the eye, spacious cards, beautiful typography, and graphics.



Jinja Material Lite - Starter provided by AppSeed.


What is Jinja

Jinja2 is a Python template 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?

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

HTML Escaping Jinja 2 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 weinherit a common structure.


How to get Jinja2

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

$pipinstalljinja2
Enter fullscreen modeExit fullscreen mode

Jinja in action

Simple runtime replace

>>>fromjinja2importTemplate>>>t=Template("Hello {{ token }}!")>>>t.render(token="Jinja2")u'Hello Jinja2!'
Enter fullscreen modeExit fullscreen mode

The engine will replace the innertoken with valueJinja2. This is quite useful when we use this block for differenttoken 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 afor structure as bellow:

# Define data structuremy_list=[0,1,2,3,4,5]# a simple list with integers
Enter fullscreen modeExit fullscreen mode

In Jinja, we can iterate with ease, using afor block:

...<ul>        {% for n in my_list %}<li>{{n}}</li>        {% endfor %}</ul>...
Enter fullscreen modeExit fullscreen mode

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 asbase.html

<html><head><title>My Jinja {% block title %}{% endblock %}</title></head><body><divclass="container"><h2>This is from the base template</h2><br>      { block content }{ endblock }<br></div></body></html>
Enter fullscreen modeExit fullscreen mode

The Child template - saved as child.html

{ extends "base.html" }{ block title } MySample { endblock }{ block content }  Cool content here{ endblock }
Enter fullscreen modeExit fullscreen mode

When Jinja loadschild.html, the { extends } block informs the engine to merge thebase.html template with the content provided bychild.html

  • { block title } becomeMySample
  • { block content } becomeCool content here

Here is the final HTML generated by Jinja:

<html><head><title>My Jinja MySample</title></head><body><divclass="container"><h2>This is from the base template</h2><br>      Cool content here<br></div></body></html>
Enter fullscreen modeExit fullscreen mode

This powerful feature helps us to build complex web apps with ease by using common pages and components to generated dynamic pages hydrated with real information loaded from the database or provided by users, for instance.


Thanks for reading! Let me know your thoughts in the comments.


Resources & Links

Top comments(5)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
moshess profile image
Mostafa Hesari
An industrial engineer and computer science enthusiast who serve the art.
  • Location
    T3hran
  • Education
    Engineering
  • Joined

I enjoyed reading this introduction.
Looking forward to read more from you.

CollapseExpand
 
sm0ke profile image
Sm0ke
#Automation, my favorite programming language
  • Location
    Constanta, Romania
  • Education
    Hack Land
  • Work
    Founder @ AppSeed.us
  • Joined

Thanks for reading Mostafa. :)

CollapseExpand
 
codeperfectplus profile image
Deepak Raj
Founder SystemGuard | Building a complete server monitoring and threat detection tool
  • Location
    India
  • Work
    Data Scinetist
  • Joined

Really helpful article on Jinja2

CollapseExpand
 
sm0ke profile image
Sm0ke
#Automation, my favorite programming language
  • Location
    Constanta, Romania
  • Education
    Hack Land
  • Work
    Founder @ AppSeed.us
  • Joined

Thank you! :)
Nunjunks is also super nice. I'm using it with11ty

CollapseExpand
 
admindashboards profile image
admin-dashboards
Open-Source and Free Admin Dashboards
  • Joined

Niceeee. Thanks for writing.
Also, samples are great.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

#Automation, my favorite programming language
  • Location
    Constanta, Romania
  • Education
    Hack Land
  • Work
    Founder @ AppSeed.us
  • Joined

More fromSm0ke

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp