将 html 模板存储在文档字符串中?

发布于 2024-10-21 16:51:22 字数 782 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短叹 2024-10-28 16:51:22

我认为这是一个糟糕的计划。

文档字符串用于文档,模板不是文档。文档应该描述该函数的用途。 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.

醉酒的小男人 2024-10-28 16:51:22

这当然是一个有趣的想法,并且按照 doctest 的示例,将功能上有用的内容而不只是文本放入文档字符串中并非完全闻所未闻。明显的缺点是文档字符串中没有文档。现在,如果这些方法不是程序员可能需要通过 help() (或使用文档字符串自动生成的文档)提供文档的内容,那么这可能是也可能不是一个大问题。

但是,为什么不呢:

  1. 只使用本地字符串变量 - 缺点是您无法从函数范围之外通过__doc__获取它
  2. >如果只使用一次,只需将其放入 render_template_string 调用 - 与 #1 相同的缺点,但如果使用多次则不适用
  3. 创建另一个将此字符串作为参数的装饰器 - 特别是如果它只是遵循上面的模式,您只使用它一次并且您总是在最后执行相同的调用,这将允许您拉动它从那个方法中

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:

  1. just use a local string variable - the downside would be that you can't get at it via __doc__ from outside the scope of the function
  2. if it's just used that once, just put it into the render_template_string call - same drawbacks as #1, but also doesn't apply if it's used more than once
  3. create another decorator that takes this string as an argument - especially if it just follows the pattern above where you're just using it the once and you're always doing the same call at the end, this would allow you to pull it out of that method
征棹 2024-10-28 16:51:22

在某些模板引擎中,如果对象具有 __html__ 方法,则其输出将被视为安全(转义)字符串。

def fn(x):
    bla = x
fn.__html__ = lambda : '''
    <h1>Headline</h1>
    <p>lorem ipsum</p>
    '''

In some templating engines, if an object has a __html__ method, it's output is treated as a safe (escaped) string.

def fn(x):
    bla = x
fn.__html__ = lambda : '''
    <h1>Headline</h1>
    <p>lorem ipsum</p>
    '''
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文