我想實現一個簡單的日曆網頁。Jinja2:正確的方式寫[不是]複雜的模板
我是很新的Jinja2,但我的模板的理解是,作爲模板設計用於此目的在Python編寫源代碼的HTML應避免。
,我要面對我,我不知道怎樣寫清楚模板這個項目的問題。我想有一個在我的項目的總體架構的問題...
我的頁面模板:
{% extends "base.html" %}
{% block title %}The title{% endblock %}
{% block content %}
<h1>Hey</h1>
<p> I'm happy </p>
<div id="calendar">
<table>
<tr>
{% for month in range(1, 13) %}
<td valign="top" align="center">{# html code for a single month goes here... #}</td>
{% endfor %}
</tr>
</table>
</div>
{% endblock %}
每個月模板
<table>
<th>{{ month_name }}</th>
{% for day_number in days %}
<tr><td>{{ day_number }}</td><td>{{ weekday }}</td></tr>
{% endfor %}
</table>
最後,我有一個Python類日曆基本上提供幫助函數來計算月的日子:
class Calendar:
def __init__(self, year):
self.year = year
def monthrange(self, month):
nextmonth = month % 12 + 1
nextyear = self.year + 1 if nextmonth == 1 else self.year
firstday = datetime.date(self.year, month, 1)
lastday = datetime.date(nextyear, nextmonth, 1)
return (1, (lastday - firstday).days)
def itermonthdates(self, month):
first, last = self.monthrange(month)
for i in range(first, last + 1):
yield datetime.date(self.year, month, i)
def tohtml(self):
def month_to_html(month):
# !!! This code generate HTML but it should not !!!
s = '<table>\n'
s += '<th>{}</th>'.format(MONTHS[month - 1])
for day in self.itermonthdates(month):
weekday = WEEKDAYS[day.weekday()]
d = {'day': day.day, 'weekday': weekday}
s += '<tr><td>%(day)02d</td><td>%(weekday)s</td></tr>\n' % d
s += '</table>\n'
return s
template_loader = jinja2.FileSystemLoader(searchpath='templates')
template_env = jinja2.Environment(loader=template_loader)
template = template_env.get_template('template.html')
print(template.render(months=[month_to_html(i) for i in range(1, 13)]))
因此t他的代碼只是部分工作,因爲我不知道如何使用Jinja2來渲染每個月。
任何幫助,將不勝感激。 本
這很不清楚你問什麼。 「每個月」都在一個單獨的頁面中?如果是這樣,請使用'{%include'path/to/file.html'%}'這應該可以做到。如果沒有,請進一步澄清。 –
我仍然不明白你到底需要什麼,試着更具體一些嗎?如果你不知道如何使頁面顯示出來。你可以在'Flask'中使用'return render_template('path/to/page.html')''。否則我不知道你在問什麼。 –
感謝您的回答。最終,幾個月將在同一頁面上顯示爲列。我的問題是:基本上,呈現HTML的循環應該在模板中,以避免在Python腳本中編寫HTML。但是,在這裏我使用自定義日曆類中的方法進行迭代。那麼做到這一點的正確方法是什麼? – blaurent