2010/01 2
mako中的%标记方式让不少编辑器抓狂,包括aptana,eclipse,textmate等。受asp的影响,<%是作为服务器脚本语言的开始标记,相应的,%>为结束标记。
在mako中,示例代码如下:
< %inherit file="/base.mako" />
< %def name="head_title()">
    rollenc拼博-PHP博客

Here is a p

在这段代码中,由于没有 %> 结束标记,编辑器将认为从一行后所有的代码均为服务器脚本,而非html代码,导致html高亮,格式等功能全部失效。
解决办法:替换<%脚本
mako支持预处理。即在mako模板引擎处理之前,自己写一个脚本来预先替换或作其他操作。将<%inherit file="/base.mako" /> 修改为 <makoinherit file="/base.mako" /> 是个不错的注意。既不违反XHTML的规定,又避免了抓狂的 <%
1. 在建立TemplateLookup对象时,增加preprocessor参数,在交给mako处理之前,还原<mako:为<%
def my_preprocessor(text): 
        text = re.sub(r'<(/?)mako:', r"<\1%", text)
        return text

lookup = TemplateLookup(
        #....
        preprocessor = my_preprocessor,
        )
若在pylons中,则修改文件:config/environment.py
# Create the Mako TemplateLookup, with the default auto-escaping
    def my_preprocessor(text): 
        text = re.sub(r'<(/?)mako:', r"<\1%", text)
        return text
    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=paths['templates'],
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8', default_filters=['escape'],
        imports=['from webhelpers.html import escape'],
        preprocessor = my_preprocessor,
        )
增加了2,3,4,11行。 2. 替换.mako模板代码中的<% , </%。替换后,上面示例代码如下:


    rollenc拼博-PHP博客

Here is a p

Defined tags for this entry: , ,

Posted by rollenc

Last modified on 2010-01-02 13:48