【问题标题】:Include snippets in template that have view-like logic在模板中包含具有类视图逻辑的片段
【发布时间】:2017-05-16 14:39:57
【问题描述】:

我正在用烧瓶平页建立一个博客。在降价博文的标题中,我按文件名列出了相关的博文。这些应显示为实际博文下方的摘录。

这是 blogpost-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 只是一个字符串列表。制作一个获取这些字符串并返回 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>

我的问题:如何在同一个模板中实现这一点?

我是否应该以某种方式尝试将相关博客帖子中的数据传递到上下文中:可能是一个字典列表?我应该使用上下文处理器来实现这一点吗?

【问题讨论】:

    标签: flask flask-flatpages


    【解决方案1】:

    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 }}
    

    您可以将链接添加到您的相关文章:

    <a href="{{ url_for('site.bloging', path=related.path) }}">{{ related.title }}</a>
    

    网站是我的蓝图。

    【讨论】:

      【解决方案2】:

      我得到了一个非常简单的答案,但要保持问题的开放性,看看是否有更优雅的解决方案。

      在视图函数中,我迭代了相关博客帖子的路径,并将博客对象添加到列表中,然后传递给模板。

      像这样:

      @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 %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-08
        • 1970-01-01
        • 2011-08-18
        • 2015-01-18
        • 1970-01-01
        • 1970-01-01
        • 2019-12-01
        • 2016-07-27
        相关资源
        最近更新 更多