epy.py is a Python implementation of ePython(embedded Python) like Perl's ePerl and Ruby's eRuby. I referenced to 30 Lines Implementation of eRuby, written by creator of Erubis)(fastest eRuby implementation) and pyTenjin(fastest python template system).
<% (python code) %>
<%= (python code) %>
<%# (python code) %>
<%=r (python code) %>
<% %>
endfor
, endif
, and end
at other template systems, and outdent one level to python code. I brought this idea form Python Server Pages.Python 2.5 (release25-maint, Jul 20 2008, 20:47:25)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import epy
>>> epython = epy.ePython('Hello, <%= progname.replace("p", "P") %>')
>>> env = dict(progname='epython')
>>> print epython.render(env)
Hello, ePython
>>> print epython.pysrc
_buf = []
_buf.append(u'Hello, ')
_buf.append(_esc(unicode(progname.replace("p", "P"))))
__result = ''.join(_buf)
<% for language in languages: %>
<p>embedded <%= language %> is e<%= language.title() %>.</p>
<% %>
<p>embedded perl is ePerl.</p>
<p>embedded ruby is eRuby.</p>
<p>embedded python is ePython.</p>
_buf = []
for language in languages:
_buf.append(u' embedded ')
_buf.append(_esc(unicode(language)))
_buf.append(u' is e')
_buf.append(_esc(unicode(language.title())))
_buf.append(u'.\n')
__result = ''.join(_buf)
<p>Acronym dictionary - <%= ac %>:<br />
<% if ac == 'DoCoMo': %>
Do Communication over the Mobile
<% elif ac == 'IBM': %>
International Business Machines
<% else: %>
A Clever Representation Of Names You Manufacture
<% %></p>
<p>Acronym dictionary - PCMCIA:<br />
A Clever Representation Of Names You Manufacture
</p>
_buf = []
_buf.append(u'<p>Acronym dictionary - ')
_buf.append(_esc(unicode(ac)))
_buf.append(u':<br />\n')
if ac == 'DoCoMo':
_buf.append(u' Do Communication over the Mobile\n')
elif ac == 'IBM':
_buf.append(u' International Business Machines\n')
else:
_buf.append(u' A Clever Representation Of Names You Manufacture\n')
_buf.append(u'</p>\n')
__result = ''.join(_buf)
Actually, epy.py's ePython implementation does not care for indent.
<% for language in languages: %>
<p>embedded <%= language %> is e<%= language.title() %>.</p>
<% %>
(Attention for indent !)
<% for language in languages: %>
<p>embedded <%= language %> is e<%= language.title() %>.</p>
<% %>
Both these code are perfectory work. So, "Syntax Surface" compatible.
Use epy.ePythonHTML. The render method of this class quote returning values, and use 'raw mode' to parts of ePython code you don't hope quoting.