Pylons + genshi

デフォルトテンプレート変数

http://pylonshq.com/docs/ja/0.9.7/views/#id8

  • c – テンプレートコンテキストオブジェクト (tmpl_context のエイリアス)
  • tmpl_context – テンプレートコンテキストオブジェクト
  • config – Pylons の PylonsConfig オブジェクト (辞書のように振る舞う)
  • g – プロジェクトのアプリケーショングローバル変数 (app_globals のエイリアス)
  • app_globals – プロジェクトのアプリケーショングローバル変数
  • h – プロジェクトの helpers モジュールへの参照
  • request – 現在のリクエストに対する Pylons の Request オブジェクト
  • response – 現在のリクエストに対する Pylons の Response オブジェクト
  • session – Pylons のセッションオブジェクト (セッションが削除 されていなければ)
  • translator – 現在のロケールに設定された Gettext translator オブジェクト
  • ungettext() – Unicode 版の gettext ngettext 関数 (単数形変換を 処理する)
  • _() – Unicode 版の gettext translate 関数
  • N_() – 文字列を翻訳対象とマークするための gettext no-op 関数。 しかし実際には翻訳はされません。

テンプレート中でcontroller名を参照

controller名
${request.environ['pylons.routes_dict']['controller']}
action名
${request.environ['pylons.routes_dict']['action']}

python - Pylons: Routes information availability from templates - Stack Overflow

http://foo.com/bar/baz?key=value

${request.url}
http://foo.com/bar/baz?key=value
The full request URL, including QUERY_STRING

${request.path}
/bar/baz
The path of the request, without host or query string

${request.path_url}
http://foo.com/bar/baz
The URL including SCRIPT_NAME and PATH_INFO, but not QUERY_STRING

http://docs.pylonsproject.org/projects/pyramid/en/1.0-branch/api/request.html

改行を<br/>にする nl2br

http://ndiblog.mezquita.jp/archives/112

<py:for each="line in message.split('\n')">  
${line}<br />  
</py:for>  

webhelpers を使うと楽

http://webhelpers.groovie.org/modules/html/converters.html
webhelpers.html.converters — WebHelpers v1.3b1 documentation

lib/helpers.py で nl2br を import

from webhelpers.html import escape, HTML, literal, url_escape
from webhelpers.html.tags import *
from webhelpers.html import *
from webhelpers.html.converters import nl2br
 ${h.nl2br(c.message)}

tmpl_context 変数の自動設定

http://pylonsbook.com/en/1.1/exploring-pylons.html#context-object

Pylons automatically sets up the c object to have any of the Routes variables that your action specified attached to it. You could therefore modify the helpers example from earlier to look like this, and it would still work in the same way:

def view(self, year, month, day):
    return "This is the page for %s/%s/%s"%(c.year, c.month, c.day)

Google グループ