2

当我写博客时,我喜欢将每篇博文分成自己的 .html 文件(可以吗?)

这可以防止文件变得太大,并且可以在需要时轻松返回并编辑以前编写的博客文章。

有时,博客文章会包含 css/js/ajax/template 变量。

但是在我的网站上,我喜欢一页上的所有博客文章(所以我可以滚动浏览它们,而不是为每篇文章转到单独的页面)

这是一个包含两篇博文的 html 文件:

{% extends "base.html" %}
{% block blog_posts %}
    <!-- links/targest for the side menu to jump to a post -->
    <li><a href="#post2">Post2 - April 2012</a></li>
    <li><a href="#post1">Post1 - Feb 2012</a></li>
{% endblock %}

{% block content %}

<div id="post1">
spam1 blah blah
</div>

<div id="post2">
spam2
</div>
{% endblock %}

在 base.html 我有类似的东西:

<div id="content-container">
        <div id="section-navigation">
            <ul>
                {% block blog_posts %}
                {% endblock %}
            </ul>
        </div>
        <div id="content">
            {% block content %}{% endblock %}
        </div>
</div>

使用 webapp2 和 jinja2 将这些博客文章拆分为单独文件的最佳方法是什么?

例如 blog1.html 可能看起来像:

{% block blog_posts %}
        <!-- links/targest for the side menu to jump to a post -->
        <li><a href="#post1">Post1 - Feb 2012</a></li>
    {% endblock %}

{% block content %}

    <div id="post1">
    spam1 blah blah
    </div>
{% endblock %}

(我希望链接和博客文章在网站上以正确的顺序显示)

我可以想到一种方法,post2 扩展 post1.html,post3 扩展 post2.html 等,但我更愿意扇出更多

“Henry 和 Kafura 在 1981 年 [2] 引入了基于信息流的软件结构度量,它测量复杂性作为扇入和扇出的函数。”

谢谢

4

3 回答 3

3

@robert king,您的设计中直接嵌入了模板中的数据。模板应该只包含视图的蓝图,并且每次都应该使用从主代码生成的新数据来呈现它们。我在这里模拟这个过程(编辑以说明使用循环来提取帖子标题,以及单个帖子的显示。):

import jinja2

# NOTE: in this template there is no data relating to specific posts.
# There are only references to data structures passed in from your main code
page_template = jinja2.Template('''
    <!-- this is a navigation block that should probably be in base.html -->
    {% block blog_posts %}
        <!-- links/targets for the side menu to jump to a post -->
        {% for post in posts %}
          <li><a href="{{ post.url }}">{{ post.title }} 
                                       - {{ post.date }}</a></li>
        {% endfor %}
    {% endblock %}

    <!-- this is a content block that should probably be in page.html -->
    {% block content %}
        <div id="post">
            <h1>{{ current.title }}</h1>
            <h2>{{ current.date }}</h2>
            <p>{{ current.content }}</p>
        </div>
    {% endblock %}
''')

# NOTE your main code would create a data structure such as this 
# list of dictionaries ready to pass in to your template
list_of_posts = [
         { 'url' : '#post1',
          'title' : 'My first post',
          'date' : 'Feb 2012',
          'content' : 'My first post is about Hello World.'},

         { 'url' : '#post2',
          'title' : 'My second post',
          'date' : 'Apr 2012',
          'content' : 'My second post is about Foo Bar.'}
         ]

# Pass in a full list of posts and a variable containing the last
# post in the list, assumed to be the most recent. 
print page_template.render(posts = list_of_posts,
                           current = list_of_posts[-1])

希望这可以帮助。

编辑另请参阅我对“网站片段 - 复合视图”的问题的回答

于 2012-04-29T07:19:49.570 回答
3

我刚刚在 jinja2 教程中找到了另一个选项。我认为我的处理程序将博客文章的文件名列表传递给我的模板,然后包含博客文章更有意义。

include - 将该文件的渲染内容返回到当前命名空间:

{% include 'header.html' %}
    <div ...
{% include 'footer.html' %}

默认情况下,包含的模板可以访问活动上下文的变量。有关导入和包含的上下文行为的更多详细信息,请参阅导入上下文行为

从 Jinja 2.2 开始,您可以将包含标记为忽略缺失,在这种情况下,如果要忽略的模板不存在,Jinja 将忽略该语句。当结合有或没有上下文时,它必须放在上下文可见性语句之前。这里有一些有效的例子:

{% include "sidebar.html" ignore missing %}
{% include "sidebar.html" ignore missing with context %}
{% include "sidebar.html" ignore missing without context %}

2.2 版中的新功能。

您还可以提供在包含之前检查是否存在的模板列表。将包含第一个存在的模板。如果给出了ignore missing,如果模板不存在,它将回退到不渲染,否则将引发异常。例子:

{% include ['page_detailed.html', 'page.html'] %}
{% include ['special_sidebar.html', 'sidebar.html'] ignore missing %}
于 2012-05-01T01:06:56.863 回答
1

当我读取原始 html 文件 (file.read()) 并将数据传递给我的模板时,它转义了所有 html。

而不是 {{data}} 我不得不使用允许原始 html 的 {{data|safe}}。

就像是:

class HomeHandler(BaseHandler):
    def get(self):
        file_names = sorted(os.listdir('blog_posts'))
        html = [open('blog_posts/%s' % fn).read() for fn in file_names]
        templates = {'html': enumerate(html)}
        self.render_template('home.html', **templates)

{% block content %}

    {% for num,data in html %}
        <div id="post{{num}}">
            {{data|safe}}
        </div>
        <br />
        <img src="http://www.sadmuffin.net/screamcute/graphics/graphics-page-divider/page-divider-007.gif" border=0>
        <br />
    {% endfor %}

{% endblock %}

(确保目录不是静态目录)

于 2012-04-30T02:18:59.827 回答