在html文件中写python语法的内容,的注意事项:
1:python程序中的变量通过下面方法传入到html :
1:得到变量的值 ,注意只是语法,没有太多的为什么
第二种: $:a
默认的python会使用web.websafe filter对变量做HTML-encoding.就是第一种方式,第二种方法不会对变量a做html-encoding
wolrd
注意:要在\ 这个符号后面马上敲enter,要不然 \的特殊含义会消失,而且会一起显示出来
美元的符号是$$
亲,上面只会显示一个$哦
上面定义的变量在这里也可以用,
例如
$limit(x)
10:var块,这是个比较难懂的东东,看下面的代码
在html中
python就可以根据这些内容做更多的处理,
True,False也是能识别的,
与builtin对应的一个概念是具体应用程序的globals变量或是函数,
如何使用这些globals变量或是函数可以被所有的html templates访问呢?
例子如下:
感觉这个函数就像是builtin的一样,
访问属性时不能用 _开头,
不能使用open,getattr,setattr这些函数
如果你的模板不小心用了上面的情况,会出现SecurityException 这个安全
异常
知道上面的事,你就可以在html中写python了,
1:python程序中的变量通过下面方法传入到html :
1:通过全局变量 :全局变量是不需要用$def with语法实现传递的,只要定义了2:使用模板的几种方法:
在html中就可以用,例子如下:
===================================================================
#模板公共变量,下面可以定义所有的html文件都要用到的变量 ,不需要复杂的
$def with (va,vb)
t_globals = {
'datestr': web.datestr,
'cookie': web.cookies,
"loginform": login,
"gposts":model.get_posts,
}
#指定模板目录,并设定公共模板,base="base"设定共用的base.html模板,
在./templates/base.html这个路径 找到这个文件
render = web.template.render('templates', base='base', globals=t_globals)
=========================================================
2:通过在python程序中在render时传入 ,例子如下:
=========================================================
在python文件中,
render=web.template.render("./")
class index:
def GET(self):
abc="world"
render.index(name=abc)
在index.html文件中:
$def with (name)
hello $name
===========================================================
可以看到上面的例子是在python文件中对index()函数传入了name,
而在index.html文件中,要定义一个临时变量,接受这个传入的变量
abc是python中的变量的名字
name是html文件中变量的名字,
在render.index(name=abc)实现了变量的传递 ,
注意:在 python中render.index(a,b)可以传递多个变量
那么在 html文件中就要声明对应的临时变量 $def with (va,vb)
===========================================================
1:直接使用html文件,并向index.html文件传入python变量 ,例子如下,3:下面是python 在html文件中的基本语法
在python中:
#templates是目录,到时把所有html文件放在templates目录下,如要用到的index.htmlrender=web.template.render("templates") class index: def GET(self): return reder.index("wolrd")
2:直接指定具体的文件,这个方法扩展行不好,
3:使用字符串hello=web.template.frender("templates/hello.html") return hello("world")
================================================================html="$def with (name)\n hello $name" hello=web.tempate.Template(html) return hello("wolrd")
可以看到调用了template的三种方法:
================================================================render=web.template.render("templates")只指定html文件的路径 render.index("world") hello=web.tempalte.frender("templates/hello.html")指定了具体的html文件 hello("world") hello=web.template.Template(string)直接把字符串传入进去, hello("world")
上面三种方法最常用的是第一中,render.index的方式,
================================================================
1:得到变量的值 ,注意只是语法,没有太多的为什么
$varible
$(varible)
${varible}
2:在html文件中创建新的变量 ,肯定是在赋值时才会创建新的变量 啊
语法如下,$ 加上空格 加上变量名,空格很重要$ bug=True
$ va=1
<div>
$var
</div>
3: 在取变量的值的时候 ,你会看到两种语法:
第一种: $a第二种: $:a
默认的python会使用web.websafe filter对变量做HTML-encoding.就是第一种方式,第二种方法不会对变量a做html-encoding
4: \ 这个符号的有点意思,会使多行的内容,只显示一行
hello \wolrd
注意:要在\ 这个符号后面马上敲enter,要不然 \的特殊含义会消失,而且会一起显示出来
5:问你个问题,如何在html文件中显示$这个符号(因为给webpy当特殊的用了)
答案很简单,输入两个$$就行了美元的符号是$$
亲,上面只会显示一个$哦
6:在html中如何写python风格的注释呢,我说的不是<!这样的注释哦>
$#这是注释,你在 浏览器 中是看不到的,webpy把这部分给filter了7:到了控制流部分了, 注意的面的i want这一句的缩进,要大于两个空格,
你用tab按键一般不会有问题$for i in range(10):
i want eat $i apple(s)
$ a=4
$while a<10:
$a
$ a+=1
$if a>10:
hell $a
$else:
keep on ,you will do it
一个for 在 html应用中的例子,这样创建一个表
<table>
$for c in ["a", "b", "c", "d"]:
<tr class="abc">
<td>$index</td>
<td>$c</td>
</tr>
</table>
8:其它一些有用的东西 如,$def
还可以在html中定义函数,这是多么方便的东西$def tr(value):
<tr>
$for i in value:
<td>
$i
</td>
</tr>
$def table(rows):
<table>
$for row in rows:
$:row
</table>
$ data=[['a', 'b', 'c'], [1, 2, 3], [2, 4, 6], [3, 6, 9] ]
$:table([tr(d) for d in data])
9:还有一个神奇的 关键字 code,所有的python代码都可以写在code 块下面:
$code:
x = "you can write any python code here"
y = x.title()
z = len(x + y)
def limit(s, width=10):
"""limits a string to the given width"""
if len(s) >= width:
return s[:width] + "..."
else:
return s
回来到html
上面定义的变量在这里也可以用,
例如
$limit(x)
10:var块,这是个比较难懂的东东,看下面的代码
在html中
$def with (title, body)
$var title: $title
$var content_type: text/html
<div id="body">
$body
</div>
在python中
>>> out = render.page('hello', 'hello world')
>>> out.title
u'hello'
>>> out.content_type
u'text/html'
>>> str(out)
'\n\n<div>\nhello world\n</div>\n'
可以看到var关键字的作用是把在 html中定义的变量,传回到python程序中,
python就可以根据这些内容做更多的处理,
11:在html文件中可以访问的builtin 函数 和变量 ,常用的函数都是
能访问的,如max,min,range,True,False也是能识别的,
与builtin对应的一个概念是具体应用程序的globals变量或是函数,
如何使用这些globals变量或是函数可以被所有的html templates访问呢?
例子如下:
import web
import markdown
globals={"markdown":markdown.markdown}
render=web.template.render("tempaltes",globals=globals)
这样在所有的html文件中都可以使用 makrdown这个函数了
感觉这个函数就像是builtin的一样,
12:出于安全考虑,下面的命令不能在html模板中出现
import ,exec访问属性时不能用 _开头,
不能使用open,getattr,setattr这些函数
如果你的模板不小心用了上面的情况,会出现SecurityException 这个安全
异常
知道上面的事,你就可以在html中写python了,