将 html 模板存储在文档字符串中?
我正在使用 Flask 为工作中的内部应用程序编写 Web 服务。许多 Web 服务 URI 返回 HTML 部分,我正在尝试找出一种干净的方法来存储 html 模板字符串。我不想将模板字符串放在单独的文件中,因为它们通常只有几行长,而且我不想有 20 个模板文件,每个文件有 3 行。我正在考虑在函数的文档字符串中定义函数的 html 模板字符串,因为我觉得这有多种用途。它将作为文档,基本上说“这就是我输出的内容”,并且使我不必将 3 行模板字符串存储在单独的文件中。这就是我要说的:
@app.route('/path/to/my/resource/<int:_id>')
def some_resource(_id):
"""
<select id="resource-{{ resource.id }}" class="resource">
{% for choice in choices %}
<option id="choice-{{ choice.id }}" value="{{ choice.id }}">
{{ choice.text }}
</option>
{% endfor %}
</select>
"""
# retrieving resource, etc...
return render_template_string(some_resource.__doc__, **kwargs)
我不知道这是否会成为维护的噩梦......有什么想法吗?
I am using Flask to write a web service for an internal app here at my work. Many of the web services URI's return HTML partials, and I am trying to figure out a clean way to store the html template strings. I don't want to put the template strings in separate files, since they are usually only a few lines long and I don't want to have 20 template files that each have 3 lines in them. I was thinking about defining a function's html template string in the docstring of the function, as I feel like that would serve multiple purposes. It would serve as documentation, basically saying "this is what I output", as well as keeping me from having to store 3-line templates strings in separate files. Here is what I am talking about:
@app.route('/path/to/my/resource/<int:_id>')
def some_resource(_id):
"""
<select id="resource-{{ resource.id }}" class="resource">
{% for choice in choices %}
<option id="choice-{{ choice.id }}" value="{{ choice.id }}">
{{ choice.text }}
</option>
{% endfor %}
</select>
"""
# retrieving resource, etc...
return render_template_string(some_resource.__doc__, **kwargs)
I don't know whether this would be a nightmare to maintain or not...any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这是一个糟糕的计划。
文档字符串用于文档,模板不是文档。文档应该描述该函数的用途。 HTML 模板是一个很糟糕的替代品。
您可以使用多行字符串来保存模板,这可能是个好主意。通过将它们制作为文档字符串,您不会获得任何好处。
I think its a bad plan.
Docstrings are for documentation, the template is not documentation. Documentation is supposed to describe what the function is being used for. An HTML template is a poor substitute for that.
You can use multi-line strings to hold your template, and thats probably a good idea. You don't gain anything by making them docstrings.
这当然是一个有趣的想法,并且按照 doctest 的示例,将功能上有用的内容而不只是文本放入文档字符串中并非完全闻所未闻。明显的缺点是文档字符串中没有文档。现在,如果这些方法不是程序员可能需要通过
help()
(或使用文档字符串自动生成的文档)提供文档的内容,那么这可能是也可能不是一个大问题。但是,为什么不呢:
__doc__
获取它render_template_string
调用 - 与 #1 相同的缺点,但如果使用多次则不适用It's certainly an interesting idea, and following the example of doctest, it's not entirely unheard of to put functionally-useful things in your docstrings instead of just text. The obvious drawback is that then there's no documentation in the docstring. Now, this may or may not be a huge problem if the methods are not something that programmers will likely need documentation on via
help()
(or by auto-generated docs using the docstrings).However, why not either:
__doc__
from outside the scope of the functionrender_template_string
call - same drawbacks as #1, but also doesn't apply if it's used more than once在某些模板引擎中,如果对象具有 __html__ 方法,则其输出将被视为安全(转义)字符串。
In some templating engines, if an object has a __html__ method, it's output is treated as a safe (escaped) string.