[PYTHON] Basic usage of Jinja2

Purpose

A memorandum of the super-basic part when using Jinja2

Premise

Python 3.5.2 Jinja2 2.9.6

procedure

Place the file like this.

File placement


./
 ├template.txt.j2
 └friend_builder.py

Template file preparation

You can embed specific parameters in the template file. It is also possible to write repetitive syntax.

text:template.txt.j2


great!
you
    {{character}}
I'm a friend who is good at!
Noshi!

Script for plunging values into a template

friend_builder.py


from jinja2 import Template, Environment, FileSystemLoader


env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('template.txt.j2')

data = {
    "character": "cuisine",
}

rendered = template.render(data)

print(str(rendered))

As the processing content,

  1. Get template.txt.j2 as a template
  2. Insert the value (cooking) into the {{character}} described in the template
  3. Render and display the result of inserting the value

I can't even try to make it simpler!

The execution result is as follows.

Output result


$ python friend_builder.py
great!
you
cuisine
I'm a friend who is good at!
Noshi!

The `cooking` is safely embedded in the `{{character}}` part of the template.

Recommended Posts

Basic usage of Jinja2
Basic usage of flask-classy
Basic usage of SQLAlchemy
Super basic usage of pytest
Basic usage of PySimple GUI
Basic usage of Pandas Summary
Basic usage of Python f-string
Basic usage of Btrfs on Arch Linux
Basic operation of pandas
Summary of pyenv usage
Usage of Python locals ()
Basic operation of Pandas
Basic knowledge of Python
Basic processing of librosa
[Introduction to Python] Basic usage of lambda expressions
Make a note of the list of basic Pandas usage
(Beginner) Basic usage of Datastore on Google App Engine
[Introduction to Python] Basic usage of the library matplotlib
[Python] Correct usage of map
Convenient usage summary of Flask
Basic flow of anomaly detection
virtualenv Basic command usage memo
(Minimal) usage of django logger
XPath Basics (1) -Basic Concept of XPath
Sample usage of Python pickle
One-liner basic graph of HoloViews
[Python] Correct usage of join
Index of certain pandas usage
[2020/06 latest version] Basic usage of python dependency management tool poetry
Basic knowledge of Linux and basic commands
Contents of HTML escaping process of Jinja2
Summary of basic implementation by PyTorch
Easy usage memo of Anaconda (conda)
About the basic type of Go
Installation and easy usage of pytest
Basic grammar of Python3 system (dictionary)
[python] Correct usage of if statement
[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
Basic study of OpenCV with Python
[Linux] Review of frequently used basic commands 2
Acquisition of past electricity usage China Electricity Edition
Scraping the usage history of the community cycle
Basic writing of various programming languages (self-memo)
The usage of TensorBoard has changed slightly
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
[Linux] Review of frequently used basic commands
Non-logical operator usage of or in python
Python Basic Course (at the end of 15)
Basic level performance evaluation of programming languages
Basic grammar of Python3 system (included notation)
How to install fabric and basic usage
BESS Development Memo # 01: BESS Installation and Basic Usage