简体   繁体   中英

“Include” in Flask Jinja results in slower template loading?

I had a template that was getting very long, so I split a section out into an associated partial template.

My long template now has a call to..

{% include "shorttemplate.html" %}

in it. This is appropriately inheriting everything I needed from the long template, but seems to produce an on-load flicker of the contents of shorttemplate--they're not there, then suddenly are. Shouldn't the inclusion be happening before the html is rendered? Why do I see this flicker?

I'm using the include command only for organizational clarity---I'm not even reusing this short subtemplate elsewhere, I just wanted to move this unit out of the long template.

Is there a way to ask all the flask code to execute before rendering the html so that I don't see this flicker?

The flicker is there because the page takes longer to load, and this doesn't depend on whether Flask code executes before or after rendering the HTML (although it happens just when you call render_template ). The browser simply shows an intermediate state that you don't normally see when pages load fast enough. If you have a problem with filckers like this, you can either

  • improve your server software stack by optimizing your code or replacing components you use,
  • improve your server hardware by throwing more resources to use (Faster CPU, more memory, faster storage), or
  • improve your client code by avoiding to reload the complete page, and using AJAX to update just the parts that need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM