0

In pystache (or most templating libraries), you can do a substitution such as this:

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

Is it possible to do the 'opposite'? i.e. return a dictionary (or more correctly, a set) with all the templates in a string?

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

Thanks.

smg
  • 1,133
  • 1
  • 11
  • 22

2 Answers2

1

I can't speak for pystache functionality, but it may not have a way to do this. Fortunately, you can do this with a simplex regex:

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

This outputs: ['{{person}}', '{{place}}']

Raceyman
  • 1,354
  • 1
  • 9
  • 12
  • Thanks. I ended up doing something similar. Was hoping to do it directly in pystache. Might submit it as an issue/request. – smg Jun 19 '15 at 21:28
0

it looks like you can parse it out like this:

parsed = pystache.parse("{{person}} in {{place}}")
print(parsed)

which gives the reuslt:

[_EscapeNode(key='person'), ' in ', _EscapeNode(key='place')]

(found in pystache docs here)

joram
  • 21
  • 2