模板法

Python模板系统的数量几乎和web框架的数量一样多(事实上,似乎许多模板系统都采用了类似web框架的特性)。以下是 templetor 系统的(代码)是网页.py.

  1. 模板系统必须 look 不错。不 <%#foo#%> 积垢。

  2. 尽可能重用Python术语和语义。

  3. 有足够的表达能力来做真正的计算。

  4. 可用于任何文本语言,而不仅仅是HTML和XML。

以及实施要求:

  1. 沙盒可以让不受信任的用户编写模板。

  2. 简单快速的实现。

就在这里。

变量替代

Look, a $string.
Hark, an ${arbitrary + expression}.
Gawk, a $dictionary[key].function('argument').
Cool, a $(limit)ing.

Stop, \$money isn't evaluated.

我们使用与(拒绝)基本相同的语义 PEP 215 . 变量可以放在文档中的任何位置。

新线抑制

If you put a backslash \
at the end of a line \
(like these) \
then there will be no newline.

呈现为全部一行。

表达

Here are some expressions:

$for var in iterator: I like $var!

$if times > max:
    Stop! In the name of love.
$else:
    Keep on, you can do it.

That's all, folks.

你所有的老朋友都在这里: if, while, for, else, break, continue, and pass also act as you'd expect. (Obviously, you can't have variables named any of these.) The Python code starts at the $ and ends at the :. The `` $``必须在行的开头,但这并不是一个负担,因为新行抑制(见上图)。

另外,我们对间距非常小心——所有的行在开始处都将不带空格。(开放式问题:如果你想在开头加空格呢?)另外,尾随空格可能会破坏代码。

Python有几个变化: forwhile 现在采取一个 else 子句,如果从未计算循环,则调用该子句。

(可能要添加的特性:循环变量的Django样式。)

评论

$# Here's where we hoodwink the folks at home:

Please enter in your deets:

CC: [       ]  $#this is the important one
SSN: $#Social Security Number#$ [       ]

注释以开头 $# 然后去 #$ 或者是线的尽头,以先者为准。

代码

注意:此功能目前尚未实现网页.pytempletor的实现。

Sometimes you just need to break out the Python.

$ mapping = {
$   'cool': ['nice', 'sweet', 'hot'],
$   'suck': ['bad', 'evil', 'awful']
$ }

Isn't that $mapping[thought]?
That's$ del mapping $ fine with me.

$ complicatedfunc()

$ for x in bugs:
    $ if bug.level == 'severe':
        Ooh, this one is bad.
        $ continue
    And there's $x...

循环体必须用4个空格缩进。

代码以 $ 还有一个空间,一直到下一个 $ 或是队伍的尽头,以先到者为准。如果 $ 是一个空间(所以 complicatedfunc 上面的内容不会像没有空格那样在屏幕上写入任何内容)。

Python集成

模板以以下行开头:

$def with (name, title, company='BigCo')

声明模板接受这些参数。(在 with 关键字很特别,比如 defif

别忘了把定义放在空格里

以下 不会工作

$def with (name,title,company='BigCo')

在Python中,模板看起来像一个接受这些参数的函数。它返回一个具有特殊属性的存储对象,该属性将其作为字符串进行计算,返回模板主体的值。存储对象中的元素是 def s和 set S

也许一个例子会使这一点更清楚。这是一个模板,“entry”:

$def with (post)

$var title: $post.title

<p>$markdown(post.body)</p>

<p class="byline">by $post.author</p>

这是另一个“基地”:

$def with (self)
<html><head>
  <title>$self.title</title>
</head><body>
<h1>$self.title</h1>

$:self
</body></html>

现在让我们假设我们在Python中编译这两个,第一个是 entry ,第二个是 base . 下面是我们如何使用它们:

print base( entry( post ) )

entry 获取参数post并返回一个对象,该对象的字符串值为一点HTML,在属性中显示post及其标题 title . base 获取此对象并将标题放在适当的位置,并在页面正文中显示页面本身。Python代码输出结果。

Where did ``markdown`` come from? It wasn't passed as an argument. 您可以将函数和变量的列表传递给模板编译器,使模板全局可用。 为什么$:自我? 见下文

下面是一个例子:

import template
render = template.render('templates/')
template.Template.globals['len'] = len

print render.base(render.message('Hello, world!'))

第一行导入templetor。第二个说明我们的模板在目录中 templates/. The third give all our templates access to the len function. The fourth grabs the template message.html, passes it the argument 'Hello, world!', passes the result of rendering it to mcitp 模板 base.html 并打印结果。(如果你的模板没有 .html.xml ,templetor仍将找到它们,但它不会自动进行HTML编码。)

关闭过滤器

默认情况下 template.render 将使用 web.websafe 筛选以进行HTML编码。要关闭它,请在$后加上一个“:”:

$:form.render()

输出来自窗体.渲染()将按原样显示。

$:fooBar    $# fooBar = <span>lorem ipsum</span>

模板中变量的输出将按原样显示。

包括/嵌套模板

如果要在另一个模板中嵌套一个模板,则 render() 调用,然后在页面中包含变量(未筛选)。在处理程序中:

print render.foo(render.bar())

或者(让事情更清楚一点):

barhtml = render.bar()
print render.foo(barhtml)

然后在模板中 foo.html

$def with (bar)
html goes here
$:bar
more html

这将取代 $:bar 的输出 render.bar() 打电话(这就是为什么一定要打电话 $: /未过滤,这样您就得到了未编码的HTML(当然,除非您需要其他东西)。可以使用相同的方法传入变量:

print render.foo(render.bar(baz), qux)

在模板栏中 (bar.html ):

$def with (baz)
bar stuff goes here + baz

在模板foo中 (foo.html ):

$def with (bar, qux)
html goes here
$:bar
Value of qux is $qux

逃逸

网页.py自动转义模板中使用的任何变量,因此,如果出于某种原因,name被设置为一个包含一些HTML的值,它将被正确转义并显示为纯文本。如果要关闭此功能,请写入$:name而不是$name。