Python template engine empy

empy installation

$ pip install empy

sample

Template file

xml:source.xml.tmplate


<?xml version="1.0" ?>
<sample>
@[if options['output_aaa']]@
  <aaa>
    <value>@(my_value)</value>
  </aaa>
@[end if]@
</sample>

Executable file

expand.py


import em

if __name__ == '__main__':
    template_data = {}
    template_data['options'] = {'output_aaa': True}
    template_data['my_value'] = 10
    with open('sample.xml.template', 'r') as f:
        data = f.read()
        expanded = em.expand(data, template_data)
    with open('sample.xml', 'w') as f:
        f.write(expanded)

File generation

$ python expane.py
$ cat sample.xml
<?xml version="1.0" ?>
<sample>
  <aaa>
    <value>10</value>
  </aaa>
</sample>

Recommended Posts

Python template engine empy
Jinja2 | Python template engine
python argparse template
[Python] Tkinter template
Competitive Pro Template (Python)
Python data analysis template
python unit test template
Python template for Codeforces-manual test-
Search engine work with python
[Python] Competitive template [At Coder]
Python Design Pattern --Template method
Web application with Python3.3.1 + Bottle (1) --Change template engine to jinja2
Python
Competitive programming, coding test template: Python3
Use Jinja2 for PasteScript template engine
Preprocessing template for data analysis (Python)
Template AtCoder ABC 179 Python (A ~ E)
template
Run Cloud Dataflow (Python) from App Engine
[Python] Run Flask on Google App Engine
Template for writing batch scripts in python
The Python project template I think of.
[Pyto] Operate iPhone Taptic Engine with Python
Use Django's ImageField on App Engine / Python