| Seth Mueller's blog | |
|---|---|
|
Home Projects Find Me Portfolio All Posts:
|
Jinja is Awesome2023-4-14While making the server for this blog using Flask (a Python library for web servers), I learned about the Jinja templating engine and how powerful it is. What it isIn any file, you can type Jinja code, and pass the file through a Python script for the code to be run/rendered and turned into normal text. These files are called templates as they can be used to generate other similar files. The templating code has many features like loops, variables, if statements, and filters (which are very similar to functions). You can find Jinja's documentation here Jinja tries to make its syntax similar to Python, which makes templates very readable. In your template, you can put Jinja statements in between {% and %}, and expressions in between {{ and }}. For example, if I was working on a website and wanted to make a list of people's names:
Which, when rendered by a script, would become:
This may not seem too useful, as it would just give the same output every time. To make it useful, Jinja allows you to pass in values to the template when you render it which makes it very useful. This is great because it allows you to generate lots of files that are all based on your template. For example, if a company wants to store readable records of their employees, they could use Jinja to generate simple paragraphs about each employee:
This template could be rendered from a complicated JSON file:
Which would make the nice and readable result:
How I use itOn the left sidebar of my site, there is a list of posts that is rendered with Jinja. To create this list I have a templates/index.html
( main.py
This will result in a nice list of the posts from newest to oldest:
Or this HTML:
And all I had to do in the template was design how each item should look and pass it a generated list of posts. Why it's great (for websites)When you generate most of the page on the server you can avoid using Javascript to make extra API calls to figure out what to put on the page. This will result in faster pages and can reduce the likelihood of bugs happening while the user is browsing the site. It also results in easy to edit scripts as templates have very little logic compared to fetching data from the server, making new elements, and putting them in the right place that you might have to do with extra Javascript. ConclusionJinja is a great tool for server-side rendering, but can also be used for many other things. It results in fast, easily editable pages, and a connection between the backend code and the generated HTML. This post was just meant to be a simple introduction to what Jinja is, but there is much more to it. If you would like to learn more I would recommend learning it alongside Flask, as it has Jinja built-in. Comments
Anna Romero on 2023-05-18
WOW COOL PAGE! VERY INFORMATIVE AND USEFUL :thumbs_up:
Luke Brittain on 2023-06-05
Anna I heavily agree. I do in fact love these types of blogs by young people nowadays. It shows they are learning and will not ruin the earth like Gorzup told me. I was very scared to be fair, but now that i know about Seth mueller i am no longer. I bet hes cute too
Jude Singh on 2023-08-11
Quite funny you say that Mr. Brittain, Gorzup told me the same thing. Im still not too sure about this Seth Mueller lad but Im sure he is great. If the prophecy of Gorzup if fulfilled, he could be a good carrier for his soul.
Thanks,
Jude Singh
Anonamoose on 2023-08-24
Seth muller is my favorite! He is so cool and stuff!
Post a comment:Please don't spam the comments, I get a notification every time. Also please be mature (Nolan) Message limit: 1500 characters, name limit: 100 characters |