2015-06-19 75 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