テンプレートエンジンMakoをWindows XPで使ってみる
Windows XPにPython 2.7.2の開発環境をインストールしたので、手始めに、PythonでHTMLファイルを生成してみることにした。テンプレートエンジンは過去に使ったことがあるMakoにした。元になるHTMLテンプレートは「XHTML/HTML+CSSスーパーレシピブック」を参考に、よくありがちな2段組のページを作った。
|
【送料無料】XHTML/HTML+CSSス-パ-レシピブック |
Makoのインストール
easy_installでさくっとインストールする。easy_installはhttp://pypi.python.org/pypi/setuptools#filesからダウンロードできる。
easy_install mako
Python 2.7の環境でMako 0.4.1がインストールされた。
HTMLファイルを生成するプログラム tmako1.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from mako.template import Template
t = Template(filename="./base_2col.tmpl",
input_encoding="utf-8",
output_encoding="utf-8",
encoding_errors="replace")
page_title = u"Makoのテスト"
menu_list = [
(u'BTY備忘録', 'https://bty.sakura.ne.jp/wp/'),
(u'Google', 'http://www.google.co.jp'),
(u'Yahoo', 'http://www.yahoo.co.jp'),
]
entry_title = u"投稿のタイトル"
s = u""
for i in range(100):
s = s + u"投稿のコンテンツだよ。"
entry_content = s
d = {
'page_title': page_title,
'menu_list': menu_list,
'entry_title': entry_title,
'entry_content': entry_content,
}
html_out = t.render(**d)
print html_out
生成するページのタイトルを page_title、コンテンツのタイトル、内容を entry_title、entry_contentとし、ページ右側に表示するメニューをmenu_listにタイトルとurlの組み合わせをタプルにしてリストを作った。これらをMakoに辞書の形にして渡してあげると、HTMLファイルを生成してくれる。
ページの生成は以下のようにリダイレクトしてファイルを作る
tmako1.py > mako_out1.html
生成したファイル mako_out1.html をさくらインターネットにアップロードした。
テンプレート base_2col.tmpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>${page_title}</title>
<style type="text/css">
<!--
#container {
width: 760px;
margin: auto;
}
#content {
width: 530px;
float: left;
margin: 30px 0;
}
#sidebar {
width: 200px;
float: right;
margin: 30px 0;
}
#foot {
clear: both;
}
#header {
width: auto;
height: 100px;
}
#header_inner {
padding: 20px;
}
#header h1 {
margin: 0;
font-size: 2em;
font-family: Verdana, Helvetica, sans-serif;
}
#header p {
margin: 0;
margin-top: 8px;
font-size: 1em;
}
.entry {
width: auto;
}
.entry h2 {
font-size: 1em;
margin: 0;
margin-bottom: 15px;
background-color: #b8e964;
line-height: 32px;
padding-left: 10px;
}
.entrytext {
font-size: 0.875em;
line-height: 1.5;
}
.entrytext p {
margin: 0;
margin-bottom: 20px;
}
.menu {
width: auto;
font-size: 0.75em;
}
.menu li {
border-bottom: solid 1px #aaaaaa;
padding-bottom: 8px;
margin-bottom: 8px;
line-height: 1.2;
list-style-type: none;
}
.menu li a {
color: #000000;
text-decoration: none;
}
.menu li a:hover {
color: #ff8800;
}
.menu ul {
margin: 0;
padding: 0;
border-top: solid 1px #aaaaaa;
padding-top: 8px;
}
#footer {
width: auto;
border-top: solid 5px #b8e964;
padding-top: 10px;
padding-bottom: 20px;
}
#footer p {
color: #000000;
font-size: 0.75em;
margin: 0;
}
-->
</style>
</head>
<body>
<div id="container">
<div id="head">
<div id="header">
<div id="header_inner">
<h1>BTY備忘録のテストサイト</h1>
<p>テンプレートエンジンMakoで生成したページです</p>
</div>
</div>
</div><!-- end of head -->
<div id="content">
<div class="entry">
<h2>${entry_title}</h2>
<div class="entrytext">
<p>${entry_content}</p>
</div>
</div>
</div><!-- end of content -->
<div id="sidebar">
<div class="menu">
<ul>
% for item in menu_list:
<li><a href="${item[1]}">${item[0]}</a></li>
% endfor
</ul>
</div>
</div><!-- end of sidebar -->
<div id="foot">
<div id="footer">
<p>Copyright © BTY備忘録, All Rights Reserved.</p>
</div>
</div><!-- end of foot -->
</div><!-- end of container -->
</body>
</html>
