SimpleTALで日本語を使うには?

PHP, Python, Ruby, SimpleTAL

08:49:24, by dozo Email , 153 words, 2611 views   Japanese (JP) del.icio.us

Pythonいいね。

(ノ・・)ン。。。。。。(((●コロコロッ

テンプレートエンジンとしてさっそく使っているのがSimpleTAL
PHPTALを使っていたころから目を付けていたが、
実際使ってみるとまぁまぁ良い。


日本語を使おうとしたら、
文字が全部?(クエスチョンマーク)になっていた。
ソースコードをUTF-8で揃えたのになんでやねん。
ちょっと調べてみると、
あっさり原因がわかった。
その内容をメモ。

使用しているサーバはXREA.COM
しかし、さくらインターネットなどの共用レンタルサーバなら大抵同じだと思う。

[More:]


●SimpleTALをインストール
執筆当時バージョンは4.1。
$ wget http://www.owlfish.com/software/simpleTAL/downloads/SimpleTAL-4.1.tar.gz
$ tar xvfz SimpleTAL-4.1.tar.gz
$ cd SimpleTAL-4.1
$ python setup.py --prefix=/virtual/hain/local



●SimpleTALで日本語を使う
ポイントはこの辺か。
PythonプログラムもテンプレートもUTF8で。
・変数値にはuを頭に付ける。
・compileHTMLTemplateにinputEncoding='UTF-8'
・expandにはoutputEncoding='UTF-8'


index.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, cgi
from simpletal import simpleTAL, simpleTALES

# create context
context = simpleTALES.Context()
context.addGlobal('title', u'日本語です' )

# set sys
sys.stdout.write('Content-Type: text/html')
sys.stdout.write("\n\n")

# set template
file = open( 'index.html', 'r')
template = simpleTAL.compileHTMLTemplate( file, inputEncoding='UTF-8')
file.close()
template.expand( context, sys.stdout, outputEncoding='UTF-8' )


index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title tal:content="title" />
</head>
<body>
<h1 tal:content="title" />
</body>
</html>



パスが間違ってなければサクッと動くと思う。


さくらインターネット


Trackback address for this post:

http://hain.jp/htsrv/trackback.php/184

Comments, Trackbacks:

No Comments/Trackbacks for this post yet...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

powered by b2evolution

shinobi

Neighbors
Relative
Favorites
PR

極論istの技術屋を始めて早幾年。 流れの速い業界の波にもまれながらも精一杯生きている様をとくとごらんあれ。

Archives
スポンサー

Latest bookmark
Search

Categories

Who's Online?
Misc
Syndicate this blog XML

Valid XHTML 1.0! Valid CSS! Valid RSS 2.0! Valid Atom 1.0!