12
12
# You should have received a copy of the GNU General Public License
13
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16
"""Support for Zope Page Templates using the simpletal library."""
16
"""Support for Zope Page Templates using the Chameleon library."""
20
19
import pkg_resources
23
from simpletal import simpleTAL, simpleTALES
25
logging.getLogger("simpleTAL").setLevel(logging.INFO)
26
logging.getLogger("simpleTALES").setLevel(logging.INFO)
22
from chameleon import PageTemplate
31
28
tinstance = _zpt_cache.get(tfile)
32
29
stat = os.stat(tfile)
33
30
if tinstance is None or tinstance.stat != stat:
31
with open(tfile) as tf:
33
text = re.sub(r'\s*\n\s*', '\n', text)
34
text = re.sub(r'[ \t]+', ' ', text)
34
35
tinstance = _zpt_cache[tfile] = TemplateWrapper(
35
simpleTAL.compileXMLTemplate(open(tfile)), tfile, stat)
36
PageTemplate(text), tfile, stat)
46
47
def expand(self, **info):
47
context = simpleTALES.Context(allowPythonPath=1)
48
for k, v in info.iteritems():
49
context.addGlobal(k, v)
50
s = StringIO.StringIO()
51
self.template.expandInline(context, s)
48
return self.template(**info)
54
50
def expand_into(self, f, **info):
55
context = simpleTALES.Context(allowPythonPath=1)
56
for k, v in info.iteritems():
57
context.addGlobal(k, v)
58
self.template.expand(context, f, 'utf-8')
51
f.write(self.template(**info).encode('UTF-8'))
74
67
package = classname[0:divider]
75
68
basename = classname[divider+1:]
77
raise ValueError, "All templates must be in a package"
70
raise ValueError("All templates must be in a package")
79
72
tfile = pkg_resources.resource_filename(
80
73
package, "%s.%s" % (basename, "pt"))