简体   繁体   English

在Jinja中创建对象/函数以更好地重用代码

[英]Creating objects/functions within Jinja to better re-use code

I'm currently working on a practice blog site to learn more about web development and I am using Flask. 我目前正在一个实践博客网站上,以了解有关Web开发的更多信息,并且我正在使用Flask。

On my site, people can create blog posts and view the posts of others. 人们可以在我的网站上创建博客文章,并查看其他人的文章。 Obviously, I want to make my code as re-usable as possible. 显然,我想使我的代码尽可能地可重用。 So right now, depending on the page, I am grabbing some number of blog posts in my routes.py, and then passing them into a number of different pages (eg home page, profile page, search page). 因此,现在,根据页面的不同,我会在routes.py中抓取一些博客文章,然后将它们传递到许多不同的页面(例如,主页,个人资料页面,搜索页面)中。

Each of these pages has its own template since they all look different. 每个页面都有自己的模板,因为它们看起来都不同。 However, the code for displaying the blog posts is the same in all of the sites, even though the blog posts themselves may differ. 但是,即使博客文章本身可能有所不同,用于显示博客文章的代码在所有站点中也相同。 Is there any way in Jinja to create an object or function (eg render_as_blog_posts()) to which I could pass in the blog posts to and run in the template? Jinja中有什么方法可以创建对象或函数(例如render_as_blog_posts()),我可以将其传递到博客帖子中并在模板中运行? For example: 例如:

routes.py route.py

def profile():
    blog_posts=profile_blog_posts
    return render_template("profile.html",posts=blog_posts)
def search():
    blog_posts=search_blog_posts
    return render_template("search.html",posts=blog_posts)

profile.html: profile.html:

<html>
<title>Profile</title>
{{ render_as_blog_posts(blog_posts) }}    
</html>

search.html: search.html:

<html>
<title>Search</title>
{{ render_as_blog_posts(blog_posts) }}    
</html>

I suppose, you're looking for the 'template inheritance' feature of Jinja2: https://jinja.palletsprojects.com/en/2.10.x/templates/#template-inheritance 我想,您正在寻找Jinja2的“模板继承”功能: https : //jinja.palletsprojects.com/en/2.10.x/templates/#template-inheritance

Basically, you can achieve what you want in the different ways: 基本上,您可以通过不同的方式实现所需的目标:

  1. Template extension: https://jinja.palletsprojects.com/en/2.10.x/templates/#base-template In this case you can define base template with common layout, common set of macroses and define special blocks in this template as a points of customization. 模板扩展: https ://jinja.palletsprojects.com/en/2.10.x/templates/#base-template在这种情况下,您可以定义具有通用布局,通用宏集的基本模板,并在此模板中将特殊blocks定义为定制点。 Then, you can extend this template and fill blocks in the way specific for derived template (see samples by the provided link) 然后,您可以extend此模板并以特定于派生模板的方式填充块(请参见提供的链接中的示例)
  2. Templates import: https://jinja.palletsprojects.com/en/2.10.x/templates/#import More specific feature which looks like exactly the same python's one. 模板导入: https : //jinja.palletsprojects.com/en/2.10.x/templates/#import更具体的功能,看起来与python的功能完全相同。 You can define special template which contains set of macros and then import this template into yours profile.html or search.html : 您可以定义包含一套特殊的模板的宏 ,然后import此模板到你的profile.htmlsearch.html

    {% import 'commmon_functions.html' as common %}

    Then you can refer functions from this file as a part of common namespace: 然后,您可以将此文件中的函数作为common命名空间的一部分进行引用:

    {{ common.render_as_blog_posts(blog_posts) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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