Changeset 14

Show
Ignore:
Timestamp:
09/15/05 13:43:51
Author:
johnny
Message:

Added option to set the charset (after finding out that lxml by default outputs
us-ascii rather than UTF-8!!!), factored the namespace out of the class into
a 'constant', added author string, changed comment so it doesn't call
templess a templating *language* anymore.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/templess.py

    r12 r14  
    55 E-mail: johnny@debris.demon.nl 
    66 
    7  A very compact, XML based templating language. It has only 4 different  
     7 A very compact, XML based templating system. It has only 4 different  
    88 directives, and doesn't allow any logic inside the templates, but 
    99 instead expects the application to provide a dict with all the data (as 
     
    2222__appname__ = 'templess' 
    2323__version__ = '0.1' 
     24__author__ = 'Guido Wesdorp <johnny@debris.demon.nl>' 
     25 
     26XMLNS = 'http://debris.demon.nl/xmlns/templess/%s' % __version__ 
    2427 
    2528class template: 
    26     def __init__(self, fp): 
     29    """A Templess template""" 
     30     
     31    charset = 'UTF-8' 
     32 
     33     
     34    def __init__(self, fp, charset=charset): 
    2735        self.tree = etree.parse(fp) 
    2836 
     
    5664    def render_to_string(self, context): 
    5765        """render the template, return a string""" 
    58         return etree.tostring(self.render(context)
     66        return etree.tostring(self.render(context), self.charset
    5967 
    6068    def process_node(self, node, context, is_root=False): 
     
    6472        """ 
    6573        self.xpe = xpe = etree.XPathEvaluator(node) 
    66         xpe.registerNamespace('templess',  
    67                         'http://debris.demon.nl/xmlns/templess/0.1') 
     74        xpe.registerNamespace('templess', XMLNS) 
    6875        # then handle templess:content ones 
    6976        while 1: 
     
    8996            if no templess:cond attr was found, just return True 
    9097        """ 
    91         attrname = '{http://debris.demon.nl/xmlns/templess/0.1}cond' 
     98        attrname = '{%s}cond' % XMLNS 
    9299        attrvalue = node.attrib.get(attrname) 
    93100        if attrvalue is None: 
     
    103110    def process_attr_node(self, node, context): 
    104111        """process the templess:attr attribute on a node""" 
    105         attrname = '{http://debris.demon.nl/xmlns/templess/0.1}attr' 
     112        attrname = '{%s}attr' % XMLNS 
    106113        attrvalue = node.attrib.get(attrname) 
    107114        if attrvalue is None: 
     
    119126            if type(value) == str: 
    120127                # plain string, use as element value 
    121                 value = unicode(value, 'UTF-8'
     128                value = unicode(value, self.charset
    122129            elif type(value) != unicode: 
    123130                # unknown type, stringify 
     
    144151        if replace: 
    145152            templessattr = 'replace' 
    146         attrname = ('{http://debris.demon.nl/xmlns/templess/0.1}%s' %  
    147                                                                 templessattr) 
     153        attrname = ('{%s}%s' % (XMLNS, templessattr)) 
    148154        contentkey = node.attrib.get(attrname) 
    149155        if contentkey is None: 
     
    161167            if type(value) == str: 
    162168                # plain string, use as element value 
    163                 value = unicode(value, 'UTF-8'
     169                value = unicode(value, self.charset
    164170            elif type(value) != unicode: 
    165171                # unknown type, stringify 
     
    195201                if type(value) == str: 
    196202                    # plain string, use as element value 
    197                     value = unicode(value, 'UTF-8'
     203                    value = unicode(value, self.charset
    198204                elif type(value) != unicode: 
    199205                    # unknown type, stringify