简体   繁体   English

Python 中的简单 HTML 模板

[英]Simple HTML template in Python

I'd like to fill a HTML template with data from Python.我想用来自 Python 的数据填充 HTML 模板。 While I don't know any HTML, a colleague of mine would prepare the HTML template whereas I provide the data.虽然我不懂任何 HTML,但我的一位同事会在我提供数据的同时准备 HTML 模板。 However, he knows only little about Python.然而,他对 Python 知之甚少。

Could you suggest a very basic template framework which handles this separation of HTML and Python nicely (ie not much special syntax in HTML; and not much HTML for me to know in the Python data code)?您能否建议一个非常基本的模板框架,它可以很好地处理 HTML 和 Python 的这种分离(即 HTML 中没有太多特殊的语法;Python 数据代码中没有太多 HTML 让我知道)? Ideally the HTML template should be very close to pure HTML.理想情况下,HTML 模板应该非常接近纯 HTML。

I will also have tables as data, so it would be nice if I didn't have to prepare HTML table code, but instead he could do it with some minimal markup logic.我还将表格作为数据,所以如果我不必准备 HTML 表格代码,但他可以用一些最小的标记逻辑来完成,那就太好了。 Ideally the system should work even if my colleague applies more advanced concepts like CSS.理想情况下,即使我的同事应用了 CSS 等更高级的概念,系统也应该可以正常工作。

I've found the extensive list , but it's really hard to judge which of the system is closest to my requirements as I don't know the technical language.我已经找到了广泛的列表,但由于我不懂技术语言,因此很难判断哪个系统最接近我的要求。

EDIT: the point is we are newbies with that, and we only need a basic solution:) - yet ideally no HTML for me and not much beyond plain HTML for him.编辑:关键是我们是新手,我们只需要一个基本的解决方案:) - 但理想情况下对我来说没有 HTML,对他来说也没有超出纯 HTML 的范围。

Well django has very nice powerful templating engine, that's purpose is separating HTML from python logic (but that would require you to use django altogether, so it might be an overkill if you just want templating).好吧, django有非常强大的模板引擎,其目的是将 HTML 与 python 逻辑分开(但这需要你完全使用 django,所以如果你只是想要模板,这可能是一种矫枉过正)。

If your templates are really easy (no loops) you might use native python string.format function (I used it in a side project that generated config files to some Fortran code, and it wasn't so bad).如果您的模板真的很简单(没有循环),您可以使用本机 python string.format函数(我在一个副项目中使用它为一些 Fortran 代码生成配置文件,而且还不错)。

Other choice might be jinja2 that is really close to django templates, but standalone and more powerful.其他选择可能是jinja2 ,它非常接近 django 模板,但独立且功能更强大。

Might I suggest looking at web2py as well?我可以建议也看看 web2py 吗? At the most basic level you could simple have your colleague create the html file then where ever you need data replace it with {{=var}} the var can be retrieved by the web2py controller which is written in a python function.在最基本的层面上,你可以简单地让你的同事创建 html 文件,然后在你需要数据的地方用 {{=var}} 替换它,var 可以由用 python 函数编写的 web2py 控制器检索。 From the function you could put together your data and return it the html view with "return dict(var=var)".从该函数中,您可以将数据放在一起并使用“return dict(var=var)”将其返回 html 视图。

If your view looked like this:如果您的视图如下所示:

<html>
    <head>
        <title>some title</title>
    </head>
    <body>
        <h1>{{=message}}</h1>
    </body>
</html>

The controller could look like this:控制器可能看起来像这样:

def index():
    message = "Hello World"
    return dict(message=message)

You could also use django, as other have mentioned but check the link for some web2py documentation您也可以使用 django,正如其他人提到的那样,但请检查一些web2py 文档的链接

The simple template in python is: https://docs.python.org/3.6/library/string.html#template-strings python中的简单模板是: https ://docs.python.org/3.6/library/string.html#template-strings

It comes with the language, no need for external libraries.它自带语言,不需要外部库。

Here is an example of how to use a Template:以下是如何使用模板的示例:

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
...
ValueError: Invalid placeholder in string: line 1, col 11
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
...
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'

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

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