I'm always excited to take on new projects and collaborate with innovative minds.

Phone

+855 12 282 686

Email

samnangrosady9@gmail.com

Social Links

Infrastructure

Jinja: The Templating Wizard That Saves Devs From Keyboard Trauma

Jinja: template engine

Jinja: The Templating Wizard That Saves Devs From Keyboard Trauma

💡 What is Jinja?

🧙‍♂️ Jinja is a modern and designer-friendly templating language for Python, used to generate files dynamically. Think of it like Mad Libs for config files, where you can inject variables into a template and programmatically generate output.

It’s used heavily in:

  • Any scenario where you want to avoid copy-paste hell 🔥
  • Infrastructure as Code (Ansible, SaltStack)
  • Web frameworks (like Flask or Django)
  • Easy to learn if you have experience some template engine such as Laravel Blade, Twig, Haml, 11ty

🎯 Why Use Jinja?

Use Jinja when you want to:

  • Reduce human errors by avoiding repetitive copy-paste
  • Dynamically change parameters like server names, ports, or environments
  • Reusable: You can write once and render thousands of variations. Efficiency FTW.
  • Clean separation: Keeps logic out of your final output, whether it’s HTML or NGINX configs.
  • CLI support (jinja-cli)
  • Make your deployments look like wizardry ✨

❌ Don't Use Jinja

When...Why
You only need one or two static filesOverkill. Just use Vim and be done.
You need real-time updates (e.g. hot reload UIs)Jinja is static. It renders once and that's it.
Your team has zero Python experienceMight be a steep ramp-up.
You want complex business logic in templatesTemplates are not codebases! 🤯

Example: Automating NGINX Config with Jinja

Let’s break it down with an imaginative metaphor:

"Using vim for 1,000 NGINX config files is like writing wedding invitations by hand. Sweet, but painful." Jinja is like a laser printer with your best handwriting.

Scenario: You run a massive fleet of servers and each one needs its own NGINX configuration:

Without Jinja:
→ vim  
 → Modify 
  → Save
→ Repeat 999 times 🔁
 → Cry 😭
With Jinja:
 
screenshot-2025-05-08-at-105253-pm.png

 You write one template:

# nginx_template.j2
upstream {{ upstream }} {
  {% for upstream_item in upstream_servers%}
    {{- upstream_item }};
  {% endfor %}
}
server {
  listen 80;
  server_name {{ domain_name }};
  rewrite ^(.*) https://$host$1 permanent;
}
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name {{ domain_name }};
  server_tokens off;
  ssl_certificate /etc/letsencrypt/live/{{ domain_name }}/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/{{ domain_name }}/privkey.pem;
  client_max_body_size 50M;
  access_log /var/log/nginx/{{ domain_name }}-access.log;
  error_log /var/log/nginx/{{ domain_name }}-error.log;
  {%- if auth %}
  auth_basic {{ auth.auth_basic }};
  auth_basic_user_file {{ auth.auth_user_file }};
  {% endif -%}
  location / {
    access_log /var/log/nginx/{{ domain_name }}-access.log;
    proxy_pass {{ upstream }};
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Port $server_port;
  }
}

With dynamic data with json

app.local.json
{
  "domain_name" : "app.local",
  "upstream" : "http://proxy-app",
  "upstream_servers" : [
    "server 10.143.41.104:8081",
    "server 10.143.41.104:8082",
    "server 10.143.41.104:8083"
  ],
  "auth" : {
    "auth_basic" : "Restricted Area",
    "auth_user_file" : "/etc/nginx/htpasswd/auth"
  }
}

Run-time 🏃

jinja2 ./nginx_template.j2 ./app.local.json --format=json --outfile=./app.local.conf

Now you’ve got a thousand configs generated faster than a barista can spell your name wrong.

Final Thoughts

Jinja is the unsung hero of DevOps, backend automation, and lazy (smart) developers everywhere. It’s not flashy, but it’s reliable, fast, and scales beautifully. 🚀

Use it wisely — and maybe, just maybe, save your fingers from vim-induced arthritis.


Full content:Dev.to
Demo:Github

3 min read
May 08, 2025
By Samnang Rosady
Share

Related posts

Aug 15, 2025 • 1 min read
Redis Commander

redis commander: redis monitoring tool

May 02, 2025 • 2 min read
A Guide to Setting Up Local HTTPS Portals with Docker

Mimicking production environments with HTTPS setups ensures more accur...

Mar 24, 2025 • 2 min read
Rclone: Cloudflare R2 and Nginx Reverse Proxy

Rclone with Cloudflare R2 and Nginx reverse proxy for enhanced cloud s...