欢迎大家赞助一杯啤酒🍺 我们准备了下酒菜:Formal mathematics/Isabelle/ML, Formal verification/Coq/Agda, C++/Erlang/Lisp
Python Templating Language
来自开放百科 - 灰狐
(版本间的差异)
小 |
|||
(未显示1个用户的6个中间版本) | |||
第1行: | 第1行: | ||
− | PTL = Python Templating Language | + | PTL = Python Templating Language, [[Python]] 模版语言 |
+ | def f [html] (): content | ||
+ | |||
+ | ==First Example== | ||
+ | touch test_page.ptl | ||
+ | def numbers [plain] (N): | ||
+ | for i in range(N): | ||
+ | i | ||
+ | " " | ||
+ | |||
+ | >>>import quixote | ||
+ | >>>quixote.enable_ptl() | ||
+ | >>>import test_page | ||
+ | >>>test_page.numbers(5) | ||
+ | ' 0 1 2 3 4 ' | ||
+ | ==Example== | ||
example.ptl: | example.ptl: | ||
− | + | To callers, templates behave like regular Python functions | |
def cell [html] (content): | def cell [html] (content): | ||
'<td>' # Literal expressions are appended to the output | '<td>' # Literal expressions are appended to the output | ||
第13行: | 第28行: | ||
'<tr>' | '<tr>' | ||
for s in L: | for s in L: | ||
+ | cell(s) | ||
+ | '</tr>\n' | ||
+ | |||
+ | def loop (n): # No [html], so this is a regular Python function | ||
+ | output = "" | ||
+ | for i in range(1, 10): | ||
+ | output += row([str(i), i*'a', i*'b']) | ||
+ | return output | ||
+ | |||
+ | ==Using templates== | ||
+ | |||
+ | Templates live in .ptl files, which can be imported. To enable this: | ||
+ | |||
+ | import quixote ; quixote.enable_ptl() # Enable import hook | ||
+ | |||
+ | Templates behave just like Python functions: | ||
+ | |||
+ | >>> import example | ||
+ | >>> example.cell('abc') | ||
+ | <htmltext '<td>abc</td>'> | ||
+ | >>> example.loop() | ||
+ | <htmltext '<tr><td>1</td><td>a</td><td>b</td>...</tr>\n'> | ||
+ | |||
+ | In .ptl files, methods can even be PTL files. | ||
+ | |||
+ | ==Automatic escaping== | ||
+ | def no_quote [plain] (arg): | ||
+ | '<title>' | ||
+ | arg # Converted to string | ||
+ | '</title>' | ||
+ | |||
+ | def quote [html] (arg): | ||
+ | '<title>' | ||
+ | arg # Converted to string and HTML-escaped | ||
+ | '</title>' | ||
+ | |||
+ | >>> no_quote('A history of the < symbol') | ||
+ | '<title>A history of the < symbol</title>' | ||
+ | >>> quote('A history of the < symbol') | ||
+ | <htmltext '<title>A history of the < symbol</title>'> | ||
+ | |||
+ | ==链接== | ||
+ | *[http://wiki.python.org/moin/Templating Templating in Python] | ||
+ | |||
+ | {{comment}} | ||
+ | |||
+ | [[Category:Template]] | ||
+ | [[Category:Python]] |
2010年12月1日 (三) 00:24的最后版本
PTL = Python Templating Language, Python 模版语言
def f [html] (): content
目录 |
[编辑] First Example
touch test_page.ptl
def numbers [plain] (N): for i in range(N): i " "
>>>import quixote >>>quixote.enable_ptl() >>>import test_page >>>test_page.numbers(5) ' 0 1 2 3 4 '
[编辑] Example
example.ptl:
To callers, templates behave like regular Python functions
def cell [html] (content): '<td>' # Literal expressions are appended to the output content # Expressions are evaluated, too. '</td>'
def row [html] (L): # L: list of strings containing cell content '<tr>' for s in L: cell(s) '</tr>\n'
def loop (n): # No [html], so this is a regular Python function output = "" for i in range(1, 10): output += row([str(i), i*'a', i*'b']) return output
[编辑] Using templates
Templates live in .ptl files, which can be imported. To enable this:
import quixote ; quixote.enable_ptl() # Enable import hook
Templates behave just like Python functions:
>>> import example >>> example.cell('abc') <htmltext '<td>abc</td>'> >>> example.loop() <htmltext '<tr><td>1</td><td>a</td><td>b</td>...</tr>\n'>
In .ptl files, methods can even be PTL files.
[编辑] Automatic escaping
def no_quote [plain] (arg): '<title>' arg # Converted to string '</title>'
def quote [html] (arg): '<title>' arg # Converted to string and HTML-escaped '</title>'
>>> no_quote('A history of the < symbol') '<title>A history of the < symbol</title>' >>> quote('A history of the < symbol') <htmltext '<title>A history of the < symbol</title>'>
[编辑] 链接
<discussion>characters_max=300</discussion>
分享您的观点