简体   繁体   English

如何将Jinja2模板输出存储为字符串

[英]How to store Jinja2 template output as a string

I want to use Jinja2 to store a template as a string variable in python instead of rendering it as a file. 我想使用Jinja2将模板存储为python中的字符串变量,而不是将其呈现为文件。 How would I go about doing this ? 我将如何去做呢?

For example using f-strings I'd do : 例如,使用f字符串,我会这样做:

template = f""" This is a {foo}"""

Unfortunately f-strings don't suit my needs hence I wanted to try out Jinja2. 不幸的是,f弦不适合我的需求,因此我想尝试Jinja2。

Edit : Requirement 编辑:要求

cars = {
  'tesla': {
    'cost': '34000',
    'length': '185',
    'range': '220',
  },
  'chevy': {
    'cost': '37000',
    'length': '134',
    'range': '238',
  }

I would like to take the values from this dictionary, insert it into template and store it as a string to be used later. 我想从此字典中获取值,将其插入模板并将其存储为字符串以供以后使用。

I tried using the below code but I get an invalid syntax error. 我尝试使用以下代码,但收到无效的语法错误。

template1 = Template("""Tesla 
                        Cost : {{ cars.tesla.cost }}""")
template2 = template1.render()

# Expected Output
print(template2)
Tesla
Cost : 34000

As its not clear what your requirement is, a simple way to store a jinja2 template in a variable is available through the Template class. 由于尚不清楚您的要求是什么,可以通过Template类提供一种将jinja2模板存储在变量中的简单方法。 The following example from the jinja2 docs shows how you can do it: jinja2文档中的以下示例显示了如何执行此操作:

from jinja2 import Template
template = Template('Hello {{ name }}!')

If I print template , it shows a Template object stored in memory: 如果我打印template ,它将显示存储在内存中的Template对象:

print(template)
#Output
<Template memory:2ea4e10>

You can pass a custom name to the render() and print the template with the value of name : 您可以将自定义名称传递给render()并使用name的值打印模板:

print(template.render(name='John Wick'))
#Output:
Hello John Wick!

A slightly more complex template can be stored in a simple variable and then passed to Template : 稍微复杂一点的模板可以存储在一个简单变量中,然后传递给Template

jinja_string = """<title>{{title}}</title>
<ul>
{% for user in users %}
  <li>{{ user }}</li>
{% endfor %}
</ul>"""

template = Template(jinja_string)
users = ["John", "Sam", "Joe"]
print(template.render(title="Users", users=users))

#Output:
<title>Users</title>
<ul>

  <li>John</li>

  <li>Sam</li>

  <li>Joe</li>

</ul>

For the expected output template: 对于预期的输出模板:

jinja_string = """{{title}}:
{% for car,value in cars.items() %}  
  Car: {{ car }}
  Cost: {{value['cost']}}
  Length: {{value['length']}}
  Range: {{value['range']}}
{% endfor %}
"""

template = Template(jinja_string)

cars = {
  'tesla': {
    'cost': '34000',
    'length': '185',
    'range': '220',
  },
  'chevy': {
    'cost': '37000',
    'length': '134',
    'range': '238',
  }
}

print(template.render(title="Cars", cars=cars))
#Output:
Cars:

  Car: tesla
  Cost: 34000
  Length: 185
  Range: 220

  Car: chevy
  Cost: 37000
  Length: 134
  Range: 238

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

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