1. <i id='hHXsQ'><tr id='hHXsQ'><dt id='hHXsQ'><q id='hHXsQ'><span id='hHXsQ'><b id='hHXsQ'><form id='hHXsQ'><ins id='hHXsQ'></ins><ul id='hHXsQ'></ul><sub id='hHXsQ'></sub></form><legend id='hHXsQ'></legend><bdo id='hHXsQ'><pre id='hHXsQ'><center id='hHXsQ'></center></pre></bdo></b><th id='hHXsQ'></th></span></q></dt></tr></i><div id='hHXsQ'><tfoot id='hHXsQ'></tfoot><dl id='hHXsQ'><fieldset id='hHXsQ'></fieldset></dl></div>
      • <bdo id='hHXsQ'></bdo><ul id='hHXsQ'></ul>

      1. <legend id='hHXsQ'><style id='hHXsQ'><dir id='hHXsQ'><q id='hHXsQ'></q></dir></style></legend>
      2. <small id='hHXsQ'></small><noframes id='hHXsQ'>

        <tfoot id='hHXsQ'></tfoot>

        换行符和破折号在 jinja 中无法正常工作

        时间:2023-09-13

        <small id='5OMbr'></small><noframes id='5OMbr'>

            • <bdo id='5OMbr'></bdo><ul id='5OMbr'></ul>

                  <tbody id='5OMbr'></tbody>
                <i id='5OMbr'><tr id='5OMbr'><dt id='5OMbr'><q id='5OMbr'><span id='5OMbr'><b id='5OMbr'><form id='5OMbr'><ins id='5OMbr'></ins><ul id='5OMbr'></ul><sub id='5OMbr'></sub></form><legend id='5OMbr'></legend><bdo id='5OMbr'><pre id='5OMbr'><center id='5OMbr'></center></pre></bdo></b><th id='5OMbr'></th></span></q></dt></tr></i><div id='5OMbr'><tfoot id='5OMbr'></tfoot><dl id='5OMbr'><fieldset id='5OMbr'></fieldset></dl></div>
                <tfoot id='5OMbr'></tfoot>

              1. <legend id='5OMbr'><style id='5OMbr'><dir id='5OMbr'><q id='5OMbr'></q></dir></style></legend>

                1. 本文介绍了换行符和破折号在 jinja 中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我怎样才能产生预期的输出?谢谢

                  How could I generate the expected output ? Thanks

                  {%- for field in fields -%}
                  
                  -
                    name: {{field}}
                    type: string
                  
                  
                  
                  {%- endfor -%}
                  

                  输出

                  -
                    name: operating revenue
                    type: string-
                    name: gross operating profit
                    type: string-
                  

                  预期输出

                  -
                    name: operating revenue
                    type: string
                  -
                    name: gross operating profit
                    type: string
                  

                  代码

                  from jinja2 import Template
                  
                  fields = ["operating revenue", "gross operating profit", "EBITDA", "operating profit after depreciation", "EBIT", "date"]
                  template_file = open('./fields_template.jinja2').read()
                  template = Template(template_file)
                  html_rendered = template.render(fields=fields)
                  print(html_rendered)
                  

                  推荐答案

                  - 删除了 Jinja 标签的 那一侧 和第一个字符之间的所有空格.您在标签的内部"使用 -,因此空格被删除到 - 字符和单词 string 之后,加入两个.删除其中一个.

                  The - removes all whitespace between that side of the Jinja tag and the first character. You are using - on the 'inside' of the tags, so whitespace is removed up to the - character and after the word string, joining up the two. Remove one or the other.

                  例如,您可以删除文本开头和结尾处的额外换行符,并从开始标记的内侧删除 -:

                  You could remove the extra newlines at the start and end of your text for example, and remove the - from the inside side of the opening tag:

                  {%- for field in fields %}
                  -
                    name: {{field}}
                    type: string
                  {%- endfor -%}
                  

                  演示:

                  >>> from jinja2 import Template
                  >>> fields = ["operating revenue", "gross operating profit", "EBITDA", "operating profit after depreciation", "EBIT", "date"]
                  >>> template_file = '''
                  ... {%- for field in fields %}
                  ... -
                  ...   name: {{field}}
                  ...   type: string
                  ... {%- endfor -%}
                  ... '''
                  >>> template = Template(template_file)
                  >>> html_rendered = template.render(fields=fields)
                  >>> print(html_rendered)
                  
                  -
                    name: operating revenue
                    type: string
                  -
                    name: gross operating profit
                    type: string
                  -
                    name: EBITDA
                    type: string
                  -
                    name: operating profit after depreciation
                    type: string
                  -
                    name: EBIT
                    type: string
                  -
                    name: date
                    type: string
                  

                  这篇关于换行符和破折号在 jinja 中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何防止 YAML 在没有新行的情况下转储长行 下一篇:kubernetes Python API 客户端:执行完整的 yaml 文件

                  相关文章

                  最新文章

                2. <legend id='eHhFw'><style id='eHhFw'><dir id='eHhFw'><q id='eHhFw'></q></dir></style></legend>

                  <small id='eHhFw'></small><noframes id='eHhFw'>

                      <tfoot id='eHhFw'></tfoot>
                      <i id='eHhFw'><tr id='eHhFw'><dt id='eHhFw'><q id='eHhFw'><span id='eHhFw'><b id='eHhFw'><form id='eHhFw'><ins id='eHhFw'></ins><ul id='eHhFw'></ul><sub id='eHhFw'></sub></form><legend id='eHhFw'></legend><bdo id='eHhFw'><pre id='eHhFw'><center id='eHhFw'></center></pre></bdo></b><th id='eHhFw'></th></span></q></dt></tr></i><div id='eHhFw'><tfoot id='eHhFw'></tfoot><dl id='eHhFw'><fieldset id='eHhFw'></fieldset></dl></div>
                      • <bdo id='eHhFw'></bdo><ul id='eHhFw'></ul>