0

我正在编写一个电子邮件客户端,我想%CURSOR从 kmail 模板中实现类似的东西:在显示生成的电子邮件消息后标记应该放置光标的位置。什么模板引擎可以做这种事情?

4

1 回答 1

0
from mako.runtime import Context
class CursorContext(Context):
    __slots__=set(('lines', 'position'))

    def __init__(self, *args, **kwargs):
        super(CursorContext, self.__class__).__init__(self, self, *args, **kwargs)
        self.lines=['']
        self.position=None

    def write(self, v):
        ls=s(v).split('\n')
        self.lines[-1]+=ls.pop(0)
        self.lines+=ls

    def _record_position(self):
        if self.position is not None:
            raise ValueError('Position already defined')
        self.position=(len(self.lines), len(self.lines[-1]) if self.lines else 0)

    def _get_position(self):
        if self.position is None:
            self._record_position()
        return self.position

<...>

context=CursorContext(**env) # env is a dictionary with template environment variables
template.render_context(context) # template is mako.template.Template object
# Now template is in context.lines and 
# cursor position can be obtained by context._get_position()
于 2012-05-06T21:43:08.497 回答