webapp2,Jinja2:如何将大型html文件切成多个html文件 [英] webapp2, Jinja2: how to cut large html file into multiple html files
问题描述
当我博客时,我喜欢将每个博客文章分成自己的.html文件(这样可以吗?)这样可以防止文件变得太大,并且如果需要,可以轻松地返回并编辑以前写过的博文。
有时博客文章将包含css / js / ajax / template变量。 p>
但是在我的网站上,我喜欢在一个页面上的所有博客文章(所以我可以滚动所有的,而不是每个帖子的单独的页面)
这是一个包含两个博文的html文件:
{%extends base.html%}
{%block blog_posts%}
<! - link / targest为侧边菜单跳转到一个帖子 - >
< li>< a href =#post2> Post2 - 2012年4月< / a>< / li>
< li>< a href =#post1> Post1 - 2012年2月< / a>< / li>
{%endblock%}
{%block content%}
< div id =post1>
spam1 blah blah
< / div>
< div id =post2>
spam2
< / div>
{%endblock%}
在base.html中我有一些类似的东西: p>
< 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侧面菜单跳转到帖子 - >
< li>< a href =#post1> Post1 - 2012年2月< / a>< / li>
{%endblock%}
{%block content%}
< div id =post1>
spam1 blah blah
< / div>
{%endblock%}
(我希望链接和博客是在网站上以正确的顺序显示)
我可以想到一种方式,其中post2扩展了post1.html,post3扩展了post2.html等,但我会喜欢更多的粉丝更多
Henry和Kafura在1981年引入了基于信息流的软件结构指标[2],其测量了风扇和扇出功能的复杂性。
谢谢
@robert king,你的设计有数据直接嵌入模板。模板应该只包含一个视图的蓝图,它们应该被渲染,每次都会从主代码生成的新数据。我在这里模拟这个过程(编辑以说明使用循环来提取帖子标题,并显示单个帖子。
import jinja2
#注意:在此模板中没有与特定帖子相关的数据。
#只从主代码中传入的数据结构引用
page_template = jinja2.Template('''
<! - 这是一个应该在base.html - >
{%block blog_posts%}
<! - 侧边菜单的链接/目标跳转到帖子>
{%的帖子在帖子%}
< li>< a href ={{post.url}}> {{post.title}}
- {{post.date}}< / a>< / li>
{%endfor%}
{%endblock%}
<! - 这是一个应该在页面中的内容块。 html - >
{%block content%}
< div id =post>
< h1> {{current.title}}< / h1>
< h2> {{current.date}}< / h2>
< p> {{current.content}}< / p>
< / div>
{%endblock%}
''')
#注意你的主要代码可以创建一个数据结构,例如这个
#的字典列表,可以传入你的模板
list_of_posts = [
{'url':'#post1',
'title ':'我的第一篇文章',
'date':'2012年2月',
'content':'我的第一篇文章是关于Hello World。',
{ 'url':'#post2',
'title':'My second post',
'date':'Apr 2012',
'content':'我的第二篇文章是关于Foo Bar。'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b#将列表中的最后一个
#
打印page_template.render(posts = list_of_posts,
current = list_of_posts [-1])$ b $ b
希望这有帮助。
编辑另请参见我对网站片段 - 复合视图
When I blog, I like to separate each blog-post into its own .html file (is that ok?)
This prevents the file getting too big, and makes it easy to go back and edit a previously written blog post if need be.
Occasionally the blog post will contain css/js/ajax/template variables.
But on my website, I like all the blog posts on one page (so I can scroll through them all, instead of going to a separate page for each post)
Here is an html file that contains two blog posts:
{% 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 %}
and in base.html I have something like:
<div id="content-container">
<div id="section-navigation">
<ul>
{% block blog_posts %}
{% endblock %}
</ul>
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
</div>
What is the best way for me to split these blog posts out into separate files using webapp2 and jinja2?
e.g. blog1.html might look like:
{% 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 %}
(And I would want the links and the blogposts to be displayed in the right order on the website)
I could think of a way of doing it where post2 extends post1.html, post3 extends post2.html etc, but I would prefer to fan out more
"Henry and Kafura introduced Software Structure Metrics Based on Information Flow in 1981[2] which measures complexity as a function of fan in and fan out."
Thanks
@robert king, your design has data embedded directly in the template. Templates should only contain the blueprint to a view, and they should be rendered with new data generated from your main code every time. I simulate this process here (Edited to illustrate the use of a loop to extract post titles, and the display of a single post.):
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])
Hope this helps.
EDIT See also my answer to a question on "Site fragments - composite views"
这篇关于webapp2,Jinja2:如何将大型html文件切成多个html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!