[PYTHON] jinja2 memorandum

Basic

Sample manma

python


>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe')
u'Hello John Doe!'

or render to {'name':'John Doe'}

for loop with ipython

python


from jinja2 import Template

str_temp = """
<title>{% block title %}{% endblock %}</title>
<ul>
{% for user in users %}
  <li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
"""[1:-1]

template = Template(str_temp)
data = [ { 'url': 'imaoka.click', 'username': 'imaoka'} ]
template.render(users=data)

> u'<title></title>\n<ul>\n\n  <li><a href="gimaoka.click">imaoka</a></li>\n\n</ul>'

Ignore {% block} ʻUsers instead of ʻuser data.append({'url': 'hogehoge' ... Add it with etc. and try it

escape

I often use it in shell scripts like {#, but it's no good {{'Problem part'}} You can escape like http://qiita.com/swfz/items/63b3036a44829f201001

block It seems to be used for template inheritance Write someday

Recommended Posts

jinja2 memorandum
Matplotlib memorandum
linux memorandum
Python memorandum
Django memorandum
Command memorandum
Python Memorandum 2
Python Jinja2
plotly memorandum
Slackbot memorandum (1)
multiprocessing memorandum
Memorandum MetaTrader 5
[Linux/LPIC] Memorandum
ShellScript memorandum
pip memorandum
Python memorandum
pydoc memorandum
python memorandum
Pandas memorandum
python memorandum
DjangoGirls memorandum
Command memorandum
Python memorandum
pandas memorandum
python memorandum
Python memorandum
Python basics memorandum
RAID type memorandum
Python pathlib memorandum
Knapsack problem memorandum
Memorandum of sed
Python memorandum (algorithm)
Linux memorandum [links]
nc command memorandum
Deep Learning Memorandum
numpy memorandum 1 / np.pad
Revit API memorandum
Memorandum conda command
Pandas operation memorandum
setuptools command memorandum
Python memorandum [links]
tslearn trial memorandum
Django's basic memorandum
Memorandum about validation