【问题标题】:Python, Pyramid, Chameleon : parse Chameleon template in stringPython,Pyramid,Chameleon:在字符串中解析 Chameleon 模板
【发布时间】:2013-03-20 17:28:16
【问题描述】:

我有一个以字符串形式存储的变色龙模板。我以这种形式得到它,因为我需要先进行自己的处理。之后,我想解析模板,可能使用'render to response':

render_to_response('templates/foo.pt',
        {'foo':1, 'bar':2},
        request=request)

但是,我不知道如何解析存储在字符串中的模板,而不是指向文件中的模板。这可能吗?

【问题讨论】:

    标签: python pyramid chameleon


    【解决方案1】:

    但是,我不知道如何解析存储在 字符串,而不是指向文件中的一个。这可能吗?

    好像是这样,根据api documentation

    from chameleon import PageTemplate
    
    t = PageTemplate('some string template')
    rendered_content = t.render(encoding='utf-8')
    

    【讨论】:

    • 谢谢布尔汉!这就是我一直在寻找的。我对您的回答做了些许改动,添加了所需的“请求”参数。
    • 你确定request?可能是您需要传递模板中的变量,因此它不仅仅是实际的 HTTP 请求。在我发布的 sn-p 中,没有要插入的变量,因此 render 没有其他参数。如果你有Say hello ${foo},那么你会将foo 的替换传递给render(encoding='utf-8',foo='bar')
    • 在我的问题(金字塔)上下文中使用模板时,传递请求是必要的。
    猜你喜欢
    • 2011-07-17
    • 1970-01-01
    • 2017-11-09
    • 2015-10-30
    • 1970-01-01
    • 2018-02-23
    • 2012-06-16
    • 2016-04-10
    • 1970-01-01
    相关资源
    最近更新 更多