简体   繁体   English

用烧瓶模板化的猎豹

[英]cheetah templating with flask

I can't seem to find any information about using the cheetah templating engine with flask. 我似乎无法找到关于使用带烧瓶的猎豹模板引擎的任何信息。 Can anyone point me to something that google can't find, or show me how to use cheetah templates in a simple flask app? 任何人都可以指出谷歌找不到的东西,或者告诉我如何在一个简单的烧瓶应用程序中使用猎豹模板?

Thanks very much in advance. 首先十分感谢。

I am no Cheetah or Flask expert, but I don't think you need any special support to make it work. 我不是Cheetah或Flask专家,但我认为您不需要任何特殊支持才能使其发挥作用。 Looking at the examples for both, I came up with this (and it seems to work fine for me). 看看两者的例子,我想出了这个(它似乎对我来说很好)。

from flask import Flask
from Cheetah.Template import Template


mainTemplate = """
<html>
    <head><title>$title</title></head>
    <body><h1>$title</h1></body>
</html>"""


app = Flask(__name__)


@app.route('/')
def main_route():
    return render(mainTemplate, {'title': 'Welcome to "/"!'})


def render(template, context):
    """Helper function to make template rendering less painful."""
    return str(Template(template, namespaces=[context]))


if __name__ == "__main__":
    app.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM