如何使用多组模板组织 Flask 应用程序的代码
我正在使用 Flask 编写一个应用程序,我想为桌面和移动浏览器生成不同的代码。恕我直言,保持应用程序代码相同并在模板级别将提供不同内容的问题推入堆栈应该是一个好主意 - 因此它本质上变成了为两个用例编写两组模板并找到一种方法的问题根据每个请求选择正确的使用方法。 我将默认的 Jinja2 模板引擎与 Flask 一起使用。
我应该提到的是,我没有使用 Flask 的经验,我在编写代码时正在学习它 - 我也将其作为练习:)
您将使用什么机制来解决这个问题并保持源代码干净尽可能?
I'm writing an application with Flask and I'd like to generate different code for desktop and mobile browsers. IMHO it should be a good idea to keep the application code identical and push the problem of serving different content down the stack at the template level - so it essentially becomes a matter of writing two sets of templates for the two use cases and finding a way to choose the correct one to use at every single request.
I'm using the default Jinja2 template engine with Flask.
I should mention that I have no experience with Flask and I'm learning my way through it while I write code - I'm taking this as an exercise too :)
What mechanism would you use to address this problem and keep source code as clean as possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回复我自己:)
我最终使用了这个解决方案:
到目前为止它似乎有效,我只是将“移动模板”放在
m/
子目录中。Replying to myself :)
I ended up using this solution:
so far it seems to work, and I just put "mobile templates" in an
m/
subdirectory.我想向您指出一个可能有所不同的方向。
许多设计师和开发人员(包括我)并不认为网站设计的未来在于将模板分开,而是拥有一个动态响应其环境的模板。也就是说,它以最适合给定显示的方式重新排序其元素。
它被称为响应式设计。我知道这可能不是您一直在寻找的解决方案,但它可能会成为一种更好的解决方案。
I would like to point you in a probably somewhat different direction.
A lot of designers and developers (me included) don't see the future of website design in having the templates separated but in having one template which responds to its environment dynamically. That is it reorders its elements in a way that suites best for the given display.
It is called responsive design. I know that this is probably not the solution you have been looking for but it could become a way better one.