简体   繁体   中英

When should you use python flask conditionals vs jinja2 html template conditionals?

什么时候在模板中使用条件,而不是仅在调用包含模板的html文件的后端函数中使用条件?

This is somewhat of a question of philosophy, and as such borders on "Opinion-based," however I feel like there's a good treatise in here somewhere about separation of business logic from data.

Templates should contain data. That data might not be static, which is when you would use template conditionals. Your page might want to look different if it's being accessed by an unauthenticated user, or your page might want to allow the user to change date formats or etc.

Backend functions should do work . This is business logic, and the conditionals here should reflect that. It wouldn't be appropriate to query the user's session to see their locale and change the way data is formatted here -- that's not business logic that's UI logic. Conditionals here should effect database lookups, or API calls, or other things that might have farther-reaching side effects than "How the user perceives the data being presented."

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