본문 바로가기
Computer Science

Jinja2 Template _ 공부(1)

by wisejlog 2021. 5. 11.

jinja.palletsprojects.com/en/2.10.x/templates/

 

Template Designer Documentation — Jinja Documentation (2.11.x)

This document describes the syntax and semantics of the template engine and will be most useful as reference to those creating Jinja templates. As the template engine is very flexible, the configuration from the application can be slightly different from t

jinja.palletsprojects.com

JinJa는 Template이라는 채워넣어야할 데이터가 있는 문서에 input을 채워 최종 html을 만들어준다.

 

Template 가져오기

- Loader 

- 여기에 다양한 Loader들이 있다. 

svn.python.org/projects/external/Jinja-1.1/docs/build/loaders.html

 

Template Loaders — Jinja Documentation

This part of the documentation explains how to use and write a template loader. This list contains the builtin loaders you can use without further modification: ChoiceLoader A loader that tries multiple loaders in the order they are given to the ChoiceLoad

svn.python.org

- FileSystemLoader

from jinja import Environment, FileSystemLoader
e = Environment(loader=FileSystemLoader('templates/'))

templates 폴더하위에 template 파일 가져오기 

 

 

- FunctionLoader 

template이 function 의 return값으로 주어지는 경우 

 

- PackageLoader 

python package 내부에서 사용하는 경우

from jinja import Environment, PackageLoader
e = Environment(loader=PackageLoader('python_package_name', 'template/path'))

 

 

templates 아래에 test.html 파일

<body>{{ message }}</body>

 

외부 폴더에 다음과 같은 make_html.py

from jinja2 import Environment, FileSystemLoader

env = Environment(loader=FileSystemLoader('templates/'))

#file name
template = env.get_template('test.html')
#render -> message value inserted to html file and return as string
template_result = template.render(message="my_test_message")

with open("results/test.html","w") as f:
	f.write(template_result)

'Computer Science' 카테고리의 다른 글

Flume Sink Types  (0) 2021.12.07
Python _ 나의 단기기억 보관함  (0) 2021.05.10
Priority Queue <E> - java 생성자  (0) 2021.03.08