-->

而webapp2,Jinja2的:如何降低大的HTML文件分成多个HTML文件(webapp2, J

2019-07-29 05:10发布

当我的博客,我喜欢每一个博客,后单独部署到自己的.html文件(就是那个好吗?)

这可以防止文件变得太大,可以很容易地回去和编辑以前写的博客文章如果需要的话。

偶尔博客文章将包含CSS / JS / AJAX /模板变量。

但是,在我的网站,我想在一个页面上的所有博客文章(这样我就可以通过他们所有的滚动而不是去一个单独的页面为每个帖子)

这里是包含两个博客文章的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等,但我宁愿扇出更

“亨利和Kafura介绍,1981年基于信息流的软件结构度量[2]衡量复杂性风扇的功能和扇出。”

谢谢

Answer 1:

@robert王,你的设计有直接在模板嵌入的数据。 模板应该包含的蓝图美景,他们应该每次都从主代码生成的新数据来呈现。 我在这里模拟这个过程( 编辑说明使用循环提取文章标题,以及单篇文章的显示。):

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])

希望这可以帮助。

编辑看到我的回答上一个问题“网站的片段-复合意见”



Answer 2:

当我读到原始的HTML文件(file.read())和数据传递给我的模板,它躲过所有的HTML。

而不是{{数据}}我不得不使用{{数据|安全}}这让原始的HTML。

就像是:

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

(请确保该目录是不是一个静态的目录)



Answer 3:

我刚刚发现在教程的Jinja2另一种选择。 我觉得它更有意义对我的处理器来传递我的模板博客文章的文件名列表,然后向包括博客文章。

包括 -返回文件到当前命名空间的呈现内容:

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

包含的模板可以访问到系统默认的活动上下文的变量。 有关进口的背景下行为的详细信息,包括看到导入上下文行为 。

从神社2.2起,您可以标记与忽略丢失在这种情况下,金贾将忽略该语句是否被忽略的模板不存在的包括。 当有或没有背景结合有上下文的知名度语句前放置。 这里是一些有效的例子:

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

新的2.2版本。

您还可以提供的是包含之前检查是否存在模板的列表。 第一个存在的模板将被包括在内。 如果忽略缺少的是给予,它会回落到渲染什么,如果模板都不存在,否则会引发异常。 例:

{% include ['page_detailed.html', 'page.html'] %}
{% include ['special_sidebar.html', 'sidebar.html'] ignore missing %}


文章来源: webapp2, Jinja2: how to cut large html file into multiple html files