You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
5.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Jinja templates"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from jinja2 import Template "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jinja\n",
"\n",
"https://jinja.palletsprojects.com/en/2.11.x/\n",
"\n",
"> Jinja is a modern and designer-friendly templating language for Python (...). It is fast, widely used and secure with the optional sandboxed template execution environment:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Basic usage of Jinja\n",
"template = Template('Hello {{ name }}!')\n",
"you = 'XPUB1'\n",
"output = template.render(name=you)\n",
"print(output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Jinja is very useful for generating HTML pages\n",
"# Let's start with a HTML snippet: \n",
"template = Template('<h1>Hello {{ name }}!</h1>')\n",
"you = 'XPUB1'\n",
"html = template.render(name=you)\n",
"print(html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# But these templates can be much longer.\n",
"# Let's now use a separate jinja-template.html file, as our template:\n",
"template_file = open('jinja-template.html').read()\n",
"template = Template(template_file)\n",
"you = 'XPUB1'\n",
"html = template.render(name=you)\n",
"print(html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For loops & if / else"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### For loops\n",
"\n",
"For loops are written in a *pythonic* way.\n",
"\n",
"What is different, is the \"closing tag\" `{% endfor %}`.\n",
"\n",
"```\n",
"{% for item in list %}\n",
" \n",
" {{ item }}\n",
" \n",
"{% endfor %}\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"template = Template('''\n",
"\n",
"<h1>Hello to</h1>\n",
"\n",
"{% for name in XPUB1 %}\n",
"\n",
" <h2>{{ name }}<h2>\n",
"\n",
"{% endfor %}\n",
"\n",
"''')\n",
"XPUB1 = ['you', 'you', 'you']\n",
"html = template.render(XPUB1=XPUB1)\n",
"print(html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Or display it in HTML here in the notebook directly :)\n",
"from IPython.core.display import HTML\n",
"HTML(html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### if / else\n",
"\n",
"Also if / else statements are written in a *pythonic* way.\n",
"\n",
"The only thing again that is new, is the \"closing tag\" `{% endif %}`.\n",
"\n",
"```\n",
"{% for item in list %}\n",
" \n",
" {% if 'hello' in item %}\n",
"\n",
" <h1>HELLO</h1>\n",
" \n",
" {% else %}\n",
" \n",
" {{ item }}\n",
" \n",
" {% endif %}\n",
" \n",
"{% endfor %}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"template = Template('''\n",
"\n",
"<h1>Hello to</h1>\n",
"\n",
"{% for name in XPUB1 %}\n",
"\n",
" {% if name == 'me' %}\n",
"\n",
" <h2 style=\"color:magenta;\">{{ name }}<h2>\n",
" \n",
" {% else %}\n",
"\n",
" <h2>{{ name }}<h2>\n",
" \n",
" {% endif %}\n",
"\n",
"{% endfor %}\n",
"\n",
"''')\n",
"XPUB1 = ['you', 'me', 'you']\n",
"html = template.render(XPUB1=XPUB1)\n",
"HTML(html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Further diving\n",
"\n",
"More syntax details can be found here: https://jinja.palletsprojects.com/en/2.11.x/templates/"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}