记一次Python 中jinja2 的基本操作

                        感谢那些伴你成长的人


 

python 中的 jinja2 的基本学习和使用

 

test.j2 文件内容

{
  "customer": "{{shopify_order.customer}}",
  "orderService": "ServiceName",
  "name":"{{shopify_order.names or ''}}"
}

这是常用的

{% for xxx in 集合 %}

{%  endfor%}

 

{% if kenny.sick %}
Kenny is sick.
{% elif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay --- so far
{% endif %}
 

 

 python 代码实现

  

from jinja2 import Environment,FileSystemLoader
import json
import ast

shopify_order = {}
shopify_order['customer']='Gavin'
name_list = ['Gavin','Peter','Zero']
shopify_order['name'] = name_list

env = Environment(loader=FileSystemLoader('D:\python_DaiMa\python_learn_github\python_exercise_record\jinja2r\json'))
template = env.get_template('test.j2')
content = template.render(shopify_order=shopify_order)

print(content)
dict_str = ast.literal_eval(content)
print(dict_str)

  运行结果的使用

{
  "customer": "Gavin",
  "orderService": "ServiceName",
  "name":""
}
{'customer': 'Gavin', 'orderService': 'ServiceName', 'name': ''}

 一些基本的使用转换

  参考文章地址 : http://www.imooc.com/article/36030

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值