【问题标题】:string.format conflcits with css tags : { } 'sstring.format 与 css 标签冲突: { } 的
【发布时间】:2014-11-13 13:23:18
【问题描述】:

这是我在我的 html 模板中替换变量的方式

a1 = '<html><title>{0}</title><body>{1}</body></html>'
 vars = ['test', 'test']
 output = a1.format(*vars)

问题是..当html中有css时,这与css标签冲突。

因为 css 包含:

 {}'s

【问题讨论】:

  • 你应该使用合适的模板引擎。
  • 小心 XSS 漏洞。
  • 你有时间解决这样的问题吗?你应该使用专门设计的工具来做你想做的事。
  • 你可以使用像{{ }}这样的双括号
  • 100% 同意@SLaks,知道双卷曲,但我根本无法克服我试图遵循的原则 :)

标签: python html python-2.7


【解决方案1】:

这个问题有几个很好的答案,但它们可能并不适用于所有情况。

  • 使用模板引擎:这是解决 OP 问题的好方法,但并非总是可以在每个环境中安装和导入像 mako 这样的非标准库。

  • 用双括号 {{ 转义大括号 {:这在某些浏览器中可能会被接受,但在其他浏览器中可能会被接受(它可能应该被接受,但可能不会被接受)。

在动态生成 html 或从文件中引入 html 时,另一个选择是简单地管理 html 输出的构建顺序——在格式操作之后执行 CSS 注入

为 CSS 准备带有占位符的模板

my_html = """<html>
<head>
    <title>My Page</title>
    <style></style>
</head>
<body>
    <p>Content = {0}</p>
</body>
</html>"""

my_css = """<style>
      body {
        background-color: gray;
      }
    </style>"""

准备 HTML 输出格式

my_page = my_html.format('foobar')

在格式化操作后注入 CSS

my_final_page = my_page.replace('<style></style>', my_css)

输出

<html>
<head>
    <title>My Page</title>
    <style>
      body {
        background-color: gray;
      }
    </style>
</head>
<body>
    <p>Content = foobar</p>
</body>
</html>

您不必使用&lt;style&gt;&lt;/style&gt; 标记对作为占位符。占位符可以是任何东西,只要它是唯一的(因此您也不会无意中替换其他东西)。

完整示例

#! /usr/bin/env python
# -*- coding: utf-8 -*-

my_html = """<html>
<head>
    <title>My Page</title>
    <style></style>
</head>
<body>
    <p>Content = {0}</p>
</body>
</html>"""

my_css = """<style>
      body {
        background-color: gray;
      }
    </style>"""

my_page = my_html.format('foobar')

my_final_page = my_page.replace('<style></style>', my_css)

print(my_final_page)

【讨论】:

    【解决方案2】:

    您可以使用 Alecxe 所说的模板或使用带有字典的格式

    a1 = '<html><title>%(title)s</title><body>%(body)s</body></html>'
    vars = {'title': 'test', 'body': 'test'}
    output = a1 % vars
    

    【讨论】:

      【解决方案3】:

      只需使用双括号将它们转义。

      喜欢,

      a1 = """div.test{{ 
       bla;
      }}"""
      

      【讨论】:

        【解决方案4】:

        替代方法。

        由于您混合了 HTML 和 Python 代码,我建议 separate the logic part and the presentation part 切换到普通的模板引擎

        使用mako 的示例。

        创建一个模板 HTML 文件,留下适当的“占位符”:

        <html>
        <head>
            <title>${title}</title>
        </head>
        <body>
            <p>${text}</p>
        </body>
        </html>
        

        然后,在 python 代码中,渲染为占位符提供值的模板:

        from mako.template import Template
        
        t = Template(filename='index.html')
        print t.render(title='My Title', text='My Text')
        

        这将打印:

        <html>
        <head>
            <title>My Title</title>
        </head>
        <body>
            <p>My Text</p>
        </body>
        </html>
        

        【讨论】:

          猜你喜欢
          • 2012-01-08
          • 2021-07-02
          • 2020-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多