codecamp

CherryPy 孩子 - 模板引擎

Kid是一个简单的模板引擎,它包含要处理的模板的名称(这是必需的)以及在呈现模板时要传递的数据的输入。

在第一次创建模板时,Kid创建了一个Python模块,可以作为模板的缓存版本。

kid.Template函数返回模板类的实例,该实例可用于呈现输出内容。

模板类提供以下命令集 -

S.No命令和描述
1.

serialize

它将输出内容作为字符串返回。

2.

generate

它将输出内容作为迭代器返回。

3.

write

它将输出内容转储到文件对象中。

这些命令使用的参数如下 -

S.No命令和描述
1.

encoding

它通知如何编码输出内容

2.

fragment

它是一个布尔值,告诉XML prolog或Doctype

3.

output

这种类型的序列化用于呈现内容

例子 (Example)

让我们举一个例子来了解kid工作方式 -

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:py = "http://purl.org/kid/ns#">
   <head>
      <title>${title}</title>
      <link rel = "stylesheet" href = "style.css" />
   </head>
   <body> 
      <p>${message}</p>
   </body>
</html>
The next step after saving the file is to process the template via the Kid engine.
import kid
params = {'title': 'Hello world!!', 'message': 'CherryPy.'}
t = kid.Template('helloworld.kid', **params)
print t.serialize(output='html')


CherryPy Atom Publishing Protocol (APP)
CherryPy 孩子的属性
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

CherryPy 一个工作应用程序

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }