terom@14: # DeGAL - A pretty simple web image gallery terom@14: # Copyright (C) 2007 Tero Marttila terom@14: # http://marttila.de/~terom/degal/ terom@14: # terom@14: # This program is free software; you can redistribute it and/or modify terom@14: # it under the terms of the GNU General Public License as published by terom@14: # the Free Software Foundation; either version 2 of the License, or terom@14: # (at your option) any later version. terom@14: # terom@14: # This program is distributed in the hope that it will be useful, terom@14: # but WITHOUT ANY WARRANTY; without even the implied warranty of terom@14: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terom@14: # GNU General Public License for more details. terom@14: # terom@14: # You should have received a copy of the GNU General Public License terom@14: # along with this program; if not, write to the terom@14: # Free Software Foundation, Inc., terom@14: # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. terom@14: # terom@14: terom@12: from mako import exceptions terom@12: from mako.lookup import TemplateLookup terom@12: terom@12: import settings, helpers terom@12: terom@12: import log terom@12: terom@12: _lookup = TemplateLookup( terom@12: directories=[settings.TEMPLATE_DIR], terom@12: module_directory='%s/cache' % settings.TEMPLATE_DIR, terom@12: output_encoding='utf-8', terom@12: filesystem_checks=False, # this may need to be changed if used in a long-term process terom@12: ) terom@12: terom@12: TEMPLATE_GLOBALS = dict( terom@12: h = helpers, terom@12: version = settings.VERSION, terom@12: ) terom@12: terom@12: class Template (object) : terom@12: def __init__ (self, name) : terom@12: self.name = name terom@12: self.tpl = _lookup.get_template("%s.%s" % (name, settings.TEMPLATE_EXT)) terom@12: terom@12: def render (self, **data) : terom@12: data.update(TEMPLATE_GLOBALS) terom@12: terom@12: try : terom@12: log.template.debug("render %s with %s", self.name, data) terom@12: return self.tpl.render(**data) terom@12: except : terom@12: data = exceptions.text_error_template().render() terom@12: log.template.error(data) terom@12: terom@12: raise terom@12: terom@12: def render_to (self, file, **data) : terom@12: fh = open(file, "w") terom@12: fh.write(self.render(**data)) terom@12: fh.close() terom@12: terom@12: # templates terom@12: gallery = Template("gallery") terom@12: image = Template("image") terom@12: