# WebpageTemplate.py: User-friendly template class targeted toward Web-page usage # and optimized for speed and efficiency. # # Copyright (C) 2008-2010 David Gaarenstroom # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA """ User friendly template class targeted towards Web-page usage and optimized for speed and efficiency. Tags can be inserted in a template HTML file in a non-intrusive way, by using specially formatted comment strings. Therefore the template-file can be viewed in a browser, even with prototype data embedded in it, which will later be replaced by dynamic content. Also, webdesigners can continue to work on the template and upload it without modification. For example: ---- template.html ---- <!-- start TITLE -->This is a default title<!-- end TITLE --> ... ---- template.html ---- Load this template using: >>> template = WebpageTemplate.WebpageTemplate("template.html") >>> template['TITLE'] = "Try me..." >>> print str(template) Try me... ... """ class WebpageTemplate: """ Load a template file and substitute tags in it. The syntax for the tags is: "" "" This way a graphics designer can create a working HTML file demonstrating what the page should look like, while the dynamic content markers do not interfere with this. Code can go back and forth between designer and programmer without modifications. Templates can be copied into new instances with some tags permanently replaced. This way, site-wide tags only have to be substituted once, while URL or session specific tags can be replaced per copied instance. This saves some extra work when serving lots of pages simultaneously. """ import re markup = re.compile( r'(?P\