2015-06-19 69 views
0

在pystache(或大多数模板库),你可以做一个替代像这样:pystache - 返回所有模板字符串

>>> print pystache.render('{{person}} in {{place}}', {'person': 'Mom', 'place': 'Spain'}) 
Mom in Spain 

是否有可能做“”相反?即用一个字符串中的所有模板返回一个字典(或更正确地说,一个集合)?

>>> templates = pystache.get_templates('{{person}} in {{place}}') 
>>> print templates 
{'person', 'place'} 

谢谢。

回答

1

我不能说pystache功能,但它可能没有办法做到这一点。幸运的是,你可以用一个单一的正则表达式做到这一点:

import re 
string = "{{person}} in {{place}}" 
matches = re.findall("({{.+?}})", string) 
print matches 

此输出:['{{person}}', '{{place}}']

+0

感谢。我最终做了类似的事情。希望直接在pystache中做到这一点。可以将它作为问题/请求提交。 – smg