Changeset 14
- Timestamp:
- 09/15/05 13:43:51
- Files:
-
- trunk/templess.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/templess.py
r12 r14 5 5 E-mail: johnny@debris.demon.nl 6 6 7 A very compact, XML based templating language. It has only 4 different7 A very compact, XML based templating system. It has only 4 different 8 8 directives, and doesn't allow any logic inside the templates, but 9 9 instead expects the application to provide a dict with all the data (as … … 22 22 __appname__ = 'templess' 23 23 __version__ = '0.1' 24 __author__ = 'Guido Wesdorp <johnny@debris.demon.nl>' 25 26 XMLNS = 'http://debris.demon.nl/xmlns/templess/%s' % __version__ 24 27 25 28 class template: 26 def __init__(self, fp): 29 """A Templess template""" 30 31 charset = 'UTF-8' 32 33 34 def __init__(self, fp, charset=charset): 27 35 self.tree = etree.parse(fp) 28 36 … … 56 64 def render_to_string(self, context): 57 65 """render the template, return a string""" 58 return etree.tostring(self.render(context) )66 return etree.tostring(self.render(context), self.charset) 59 67 60 68 def process_node(self, node, context, is_root=False): … … 64 72 """ 65 73 self.xpe = xpe = etree.XPathEvaluator(node) 66 xpe.registerNamespace('templess', 67 'http://debris.demon.nl/xmlns/templess/0.1') 74 xpe.registerNamespace('templess', XMLNS) 68 75 # then handle templess:content ones 69 76 while 1: … … 89 96 if no templess:cond attr was found, just return True 90 97 """ 91 attrname = '{ http://debris.demon.nl/xmlns/templess/0.1}cond'98 attrname = '{%s}cond' % XMLNS 92 99 attrvalue = node.attrib.get(attrname) 93 100 if attrvalue is None: … … 103 110 def process_attr_node(self, node, context): 104 111 """process the templess:attr attribute on a node""" 105 attrname = '{ http://debris.demon.nl/xmlns/templess/0.1}attr'112 attrname = '{%s}attr' % XMLNS 106 113 attrvalue = node.attrib.get(attrname) 107 114 if attrvalue is None: … … 119 126 if type(value) == str: 120 127 # plain string, use as element value 121 value = unicode(value, 'UTF-8')128 value = unicode(value, self.charset) 122 129 elif type(value) != unicode: 123 130 # unknown type, stringify … … 144 151 if replace: 145 152 templessattr = 'replace' 146 attrname = ('{http://debris.demon.nl/xmlns/templess/0.1}%s' % 147 templessattr) 153 attrname = ('{%s}%s' % (XMLNS, templessattr)) 148 154 contentkey = node.attrib.get(attrname) 149 155 if contentkey is None: … … 161 167 if type(value) == str: 162 168 # plain string, use as element value 163 value = unicode(value, 'UTF-8')169 value = unicode(value, self.charset) 164 170 elif type(value) != unicode: 165 171 # unknown type, stringify … … 195 201 if type(value) == str: 196 202 # plain string, use as element value 197 value = unicode(value, 'UTF-8')203 value = unicode(value, self.charset) 198 204 elif type(value) != unicode: 199 205 # unknown type, stringify
