@@ -3054,8 +3054,8 @@ <h2>Why are template engines important?</h2> |
3054 | 3054 | with the exception of the <code><meta name="description"...</code> line which provides |
3055 | 3055 | a unique short description of what the individual page contains.</p> |
3056 | 3056 | <p>The <a href="https://github.com/makaimc/fullstackpython.com/blob/gh-pages/source/theme/templates/base.html">base.html Jinja template</a> used to generate Full Stack Python |
3057 | | -allows every page on Full Stack Python to have consistent HTML but |
3058 | | -dynamically generate the pieces that should change between pages when |
| 3057 | +allows every page on the site to have consistent HTML but |
| 3058 | +dynamically generate the pieces that need to change between pages when |
3059 | 3059 | the <a href="/static-site-generator.html">static site generator</a> executes. The below |
3060 | 3060 | code from the <code>base.html</code> template shows that the meta description is up to child |
3061 | 3061 | templates to create.</p> |
@@ -3076,18 +3076,22 @@ <h2>Why are template engines important?</h2> |
3076 | 3076 | generate the HTML output response when an HTTP request comes in for a |
3077 | 3077 | particular URL. </p> |
3078 | 3078 | <h2>Python template engines</h2> |
3079 | | -<p>There are several popular Python template engines. Template engines have to |
3080 | | -walk the spectrum between allowing arbitrary code execution and granting only |
3081 | | -a limited set of capabilities via template tags.</p> |
| 3079 | +<p>There are several popular Python template engines. A template engine implementation |
| 3080 | +will fall somewhere on the spectrum between allowing arbitrary code execution and |
| 3081 | +granting only a limited set of capabilities via template tags. A rough visual of |
| 3082 | +the code in template spectrum can be seen below for four of the major Python |
| 3083 | +template engines.</p> |
| 3084 | +<p><img src="theme/img/template-logic-spectrum.png" width="100%" alt="." class="technical-diagram" style="border-radius: 5px;"></p> |
3082 | 3085 | <h3>Jinja</h3> |
3083 | | -<p><a href="http://jinja.pocoo.org/">Jinja</a>, also known as "Jinja2" is a popular Python |
| 3086 | +<p><a href="http://jinja.pocoo.org/">Jinja</a>, also known as "Jinja2", is a popular Python |
3084 | 3087 | template engine written as an independent open source project, unlike some |
3085 | 3088 | template engines that are provided as part of a larger web framework.</p> |
3086 | 3089 | <p>Major Python open source applications such as the |
3087 | 3090 | <a href="/configuration-management.html">configuration management</a> tools Ansible |
3088 | 3091 | and <a href="https://github.com/saltstack/salt">SaltStack</a> |
3089 | 3092 | as well as the <a href="/static-site-generator.html">static site generator</a> Pelican use |
3090 | 3093 | the Jinja template engine by default for generating output files.</p> |
| 3094 | +<p>There's a whole lot more to learn about Jinja on the <a href="/jinja2.html">Jinja2</a> page.</p> |
3091 | 3095 | <h3>Django templating</h3> |
3092 | 3096 | <p>Django comes with its |
3093 | 3097 | <a href="https://docs.djangoproject.com/en/dev/topics/templates/">own template engine</a> |
@@ -3485,6 +3489,57 @@ <h3>Static site generator resources</h3> |
3485 | 3489 | is a tutorial I wrote to use the Full Stack Python source code to create |
3486 | 3490 | and deploy your first static site.</p> |
3487 | 3491 | </li> |
| 3492 | +</ul> |
| 3493 | + <h1>Jinja2</h1> |
| 3494 | +<p>Jinja2 is a Python <a href="/template-engines.html">template engine</a> used to create |
| 3495 | +HTML, XML or other markup formats that are returned to the user via an |
| 3496 | +HTTP response.</p> |
| 3497 | +<h2>Why is Jinja2 useful?</h2> |
| 3498 | +<p>Jinja2 didn't invent the templating concept. In fact, Jinja2's syntax is |
| 3499 | +inspired by Django's built-in template engine, which was released years |
| 3500 | +earlier. There were many template engines, such as <a href="https://en.wikipedia.org/wiki/JavaServer_Pages">JavaServer Pages (JSPs)</a> </p> |
| 3501 | +<p>Jinja2 is most useful because it has consistent template tag syntax and the |
| 3502 | +project is cleanly extracted as |
| 3503 | +<a href="https://github.com/mitsuhiko/jinja2">an independent open source project</a> so |
| 3504 | +it can be used a dependency by other code libraries.</p> |
| 3505 | +<p>Jinja2 strikes a thoughtful balance on the template engine spectrum where |
| 3506 | +on one end you can embed arbitrary code in the templates and the other |
| 3507 | +end a developer can code whatever she wants.</p> |
| 3508 | +<h2>What projects depend on Jinja2?</h2> |
| 3509 | +<p>Jinja2 is a commonly-used templating engine for |
| 3510 | +<a href="/web-frameworks.html">web frameworks</a> such as <a href="/flask.html">Flask</a>, |
| 3511 | +<a href="/bottle.html">Bottle</a> <a href="/morepath.html">Morepath</a> and, as of its 1.8 update, |
| 3512 | +optionally <a href="/django.html">Django</a> as well. Jinja2 is also used as a template |
| 3513 | +language by <a href="/configuration-management.html">configuration management</a> tool |
| 3514 | +Ansible and the <a href="/static-site-generators.html">static site generator</a> Pelican, |
| 3515 | +among many other similar tools.</p> |
| 3516 | +<p>The idea is that if a developer already knows Jinja2 from working with one |
| 3517 | +project then the exact same syntax and style can be used in another project |
| 3518 | +that requires templating. The re-use reduces the learning curve and saves the |
| 3519 | +open source project author from having to reinvent a new templating style.</p> |
| 3520 | +<h3>Jinja2 resources</h3> |
| 3521 | +<ul> |
| 3522 | +<li> |
| 3523 | +<p>Real Python has a nice |
| 3524 | + <a href="https://realpython.com/blog/python/primer-on-jinja-templating/">Jinja2 primer</a> |
| 3525 | + with many code examples to show how to use the template engine.</p> |
| 3526 | +</li> |
| 3527 | +<li> |
| 3528 | +<p>The <a href="http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ii-templates">second part of the Flask mega tutorial</a> |
| 3529 | + is all about Jinja2 templates. It walks through control flow, template inheritance |
| 3530 | + and other standard features of the engine.</p> |
| 3531 | +</li> |
| 3532 | +<li> |
| 3533 | +<p><a href="http://jonathanchu.is/posts/upgrading-jinja2-templates-django-18-with-admin/">Upgrading to Jinja2 Templates in Django 1.8 With Admin</a> |
| 3534 | + shows how to fix an issue that can occur with Django 1.8 and using Jinja2 as |
| 3535 | + the template engine.</p> |
| 3536 | +</li> |
| 3537 | +<li> |
| 3538 | +<p>The official |
| 3539 | + <a href="http://jinja.pocoo.org/docs/dev/templates/">Jinja2 template designer documentation</a> |
| 3540 | + is exceptionally useful both as a reference as well as a full read-through |
| 3541 | + to understand how to properly work with template tags.</p> |
| 3542 | +</li> |
3488 | 3543 | </ul> |
3489 | 3544 | <h1>Data</h1> |
3490 | 3545 | <p>Data is an incredibly broad topic but it can be broken down into many |
|