在模板中包含具有 view-like 逻辑的片段
Include snippets in template that have view-like logic
我正在用 flask-flatpages 建立博客。在 markdown 博文的 header 中,我按文件名列出了相关的博文。这些应该显示为实际博文下方的摘录。
博文-1.md 应该是这样的:
title: "Blogpost one"
published: 2014-02-13
related:
- blogpost-2.md
- blogpost-4.md
description: "This is the excerpt of blogpost one."
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
vel leo turpis. Cras vulputate mattis dignissim. Aliquam eget
purus purus.
而我想要的结果:
BLOGPOST ONE
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel
leo turpis. Cras vulputate mattis dignissim. Aliquam eget purus purus.
related posts:
BLOGPOST TWO
Summary here
BLOGPOST THREE
Also a summary
重要的部分是遵循相关博文的路径并呈现它们的标题和例外情况。天真地像:
{% for item in blog.meta.related %}
<div>
<h4>{{ item.title }}</h4>
<p>{{ item.decription</p>
</div>
{% endfor %}
这显然是行不通的,因为meta.related
只是一个字符串列表。制作一个接受这些字符串和 returns 一个 httpResponse:
的视图函数也不难
# app.py
@app.route('/excerpt/<path:path>.html')
def excerpt(path):
blog = blogs.get_or_404(path)
return render_template('excerpt.html', blog=blog)
# excerpt.html
<div>
<h4>{{ blog.meta.title }}</h4>
<p>{{ blog.meta.description }}</p>
</div>
我的问题:如何在同一个模板中实现这一点?
我是否应该以某种方式尝试将相关博客文章中的数据传递到上下文中:也许是字典列表?我应该使用上下文处理器来实现吗?
我得到了一个非常简单的答案,但让这个问题保持开放状态,看看是否有更优雅的解决方案。
在视图函数中,我迭代了相关博客文章的路径并将博客对象添加到列表中,该列表被传递到模板。
像这样:
@app.route('/blog/<path:path>.html')
def blog_detail(path):
blog = blogs.get_or_404(path)
related_list = []
for path in blog.meta['related']:
related_list.append(blogs.get_or_404(path))
return render_template('blog-detail.html', blog=blog, related_list=related_list)
并且在模板中:
{% for related in related_list %}
hoi
<div>
<h4>{{ related.meta.title }}</h4>
<p>{{ related.meta.description }}</p>
</div>
{% endfor %}
Hei Roy,感谢你的研究,但目前我无法发表评论,所以我必须找到一种方法来澄清这一点。
为了让它工作,在你写的降价文件中:
title: "Blogpost one"
published: 2014-02-13
related:
- blogpost-2.md
- blogpost-4.md
description: "This is the excerpt of blogpost one."
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
vel leo turpis. Cras vulputate mattis dignissim. Aliquam eget
purus purus.
需要更改 blogpost-2.md 和 blogpost-4.md 不带 .md 扩展名。
在视图文件上循环时没有这些更改:
for path in blog.meta['related']:
related_list.append(blogs.get_or_404(path))
blogpost-1/2.md 不会添加到您的列表中,因为 Flask-FlatPages 无法理解 .md 扩展名并且会出现 404 错误。
以我为例
@app.route('/blog/<path:path>.html')
没有 .html 就像 :
@site.route('/bloging/<path:path>/')
并为了简化一些编码,在模板中
{{ related.meta.title }}
等同于
{{ related.title }}
您可以将 link 添加到您的相关文章中:
<a href="{{ url_for('site.bloging', path=related.path) }}">{{ related.title }}</a>
网站是我的蓝图。
我正在用 flask-flatpages 建立博客。在 markdown 博文的 header 中,我按文件名列出了相关的博文。这些应该显示为实际博文下方的摘录。
博文-1.md 应该是这样的:
title: "Blogpost one"
published: 2014-02-13
related:
- blogpost-2.md
- blogpost-4.md
description: "This is the excerpt of blogpost one."
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
vel leo turpis. Cras vulputate mattis dignissim. Aliquam eget
purus purus.
而我想要的结果:
BLOGPOST ONE
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel
leo turpis. Cras vulputate mattis dignissim. Aliquam eget purus purus.
related posts:
BLOGPOST TWO
Summary here
BLOGPOST THREE
Also a summary
重要的部分是遵循相关博文的路径并呈现它们的标题和例外情况。天真地像:
{% for item in blog.meta.related %}
<div>
<h4>{{ item.title }}</h4>
<p>{{ item.decription</p>
</div>
{% endfor %}
这显然是行不通的,因为meta.related
只是一个字符串列表。制作一个接受这些字符串和 returns 一个 httpResponse:
# app.py
@app.route('/excerpt/<path:path>.html')
def excerpt(path):
blog = blogs.get_or_404(path)
return render_template('excerpt.html', blog=blog)
# excerpt.html
<div>
<h4>{{ blog.meta.title }}</h4>
<p>{{ blog.meta.description }}</p>
</div>
我的问题:如何在同一个模板中实现这一点?
我是否应该以某种方式尝试将相关博客文章中的数据传递到上下文中:也许是字典列表?我应该使用上下文处理器来实现吗?
我得到了一个非常简单的答案,但让这个问题保持开放状态,看看是否有更优雅的解决方案。
在视图函数中,我迭代了相关博客文章的路径并将博客对象添加到列表中,该列表被传递到模板。
像这样:
@app.route('/blog/<path:path>.html')
def blog_detail(path):
blog = blogs.get_or_404(path)
related_list = []
for path in blog.meta['related']:
related_list.append(blogs.get_or_404(path))
return render_template('blog-detail.html', blog=blog, related_list=related_list)
并且在模板中:
{% for related in related_list %}
hoi
<div>
<h4>{{ related.meta.title }}</h4>
<p>{{ related.meta.description }}</p>
</div>
{% endfor %}
Hei Roy,感谢你的研究,但目前我无法发表评论,所以我必须找到一种方法来澄清这一点。
为了让它工作,在你写的降价文件中:
title: "Blogpost one"
published: 2014-02-13
related:
- blogpost-2.md
- blogpost-4.md
description: "This is the excerpt of blogpost one."
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer
vel leo turpis. Cras vulputate mattis dignissim. Aliquam eget
purus purus.
需要更改 blogpost-2.md 和 blogpost-4.md 不带 .md 扩展名。 在视图文件上循环时没有这些更改:
for path in blog.meta['related']:
related_list.append(blogs.get_or_404(path))
blogpost-1/2.md 不会添加到您的列表中,因为 Flask-FlatPages 无法理解 .md 扩展名并且会出现 404 错误。
以我为例
@app.route('/blog/<path:path>.html')
没有 .html 就像 :
@site.route('/bloging/<path:path>/')
并为了简化一些编码,在模板中
{{ related.meta.title }}
等同于
{{ related.title }}
您可以将 link 添加到您的相关文章中:
<a href="{{ url_for('site.bloging', path=related.path) }}">{{ related.title }}</a>
网站是我的蓝图。