Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Richard Hriech
Richard Hriech
12,001 Points

What should I use? Jinja's {% extend ... %} or an AJAX request?

So if I'm building a Flask web app, where the site has a static navbar, sidebar and a main page content that changes when the user clicks on a link, which is the way to go?

  1. Making a "layout.html" with a {% block content %} and the subpages of the site extend this layout and change the content,

or

  1. Not using Jinja's templating, but requesting a html snippet from the server with an AJAX get request, and only changing the <div> of the main content while the navbar and other static stuff remain untouched and don't get refreshed.

Is the AJAX way faster or more data usage friendly? Or the browsers are clever enough to cache the navbar, css, and scripts and they don't reload them every time I go to a subpage?

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 27,918 Points

Personally, I'd go with the Jinja approach. Since we are already doing the majority of the work on the server-side, why not extend a base-layout. Of course, I am weak at Javascript ;-)

Pros:

  1. If you want to change the layout, you only have to do it in 1 file (admittedly, well-crafted AJAX is nice and modular too!)
  2. If you have a client that doesn't support javascript, Jinja will produce the static pages.

That being said... there is a new trend of doing "serverless" pages-- but that discussion is better left for another day!

Cheers