LoginSignup
4

More than 5 years have passed since last update.

PyramidのテンプレートをMakoに切り替える

Last updated at Posted at 2013-02-15

以下の情報は当時最新バージョンだった1.4の場合です。
1.5以降の場合はPython_Pyramid: Pyramid 1.5 mako -> jinja2を参照してください。


Pyramid Documentation 日本語版には書いてあるのですが、すぐに見つからなかったので忘備録的に書いておく。


設定方法

設定としては、development.iniもしくはproduction.iniの[app:main]に以下を追加します。

mako.directories = mypackage:templates

それに加えて、テンプレートの拡張子がデフォルトは~.ptになっているため、~.makに変更します。

例えば、

pcreate -s starter ajax_test

というようにscaffoldで作成した場合、作成されたdevelopment.iniに対して、以下のように設定します。

development.ini
###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
###

[app:main]
use = egg:ajax_test

pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = jp
pyramid.includes = pyramid_debugtoolbar

# この部分!
mako.directories = ajax_test:templates


# By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'.
# debugtoolbar.hosts = 127.0.0.1 ::1

###
# wsgi server configuration
###

[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543

###
# logging configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
###

[loggers]
keys = root, ajax_test

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console

[logger_ajax_test]
level = DEBUG
handlers =
qualname = ajax_test

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

mypackage:templatesがajax_test:templatesと自分のプロジェクト名に置き換わる感じ。
Prodaution.iniも同様に追加しておけばOK。後は必要に応じて~.makのファイルを作成し、プログラム中に指定しておけば読み込まれます。

設定する値は書かれていても、どのファイルにどの位置に設定すればいいかとか細かいところが書かれていないので、戸惑う人は多いんじゃないかなぁ。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4