2013-03-20 67 views
7

Python中的变量:在jinja2的列表中添加每个字符串的配额?

names = ["a", "b"] 

我现在写的Jinja2模板是什么:

c({{ names | join(",") }}) 

我开始使用什么上面的模板:

c(a, b) 

不过,我真正需要的是:

c("a", "b") 

我检查了Jinja2的文件,但没有找到一个过滤器来做到这一点。 Jinja2有没有人对此有过想法?

+0

为什么你需要做到这一点?如果这是JavaScript代码,你考虑过JSONP吗? – Blender 2013-03-20 02:51:48

+0

@Blender它是R代码.. – 2013-03-20 03:06:17

回答

5
为Jinja2的

使用自定义过滤器:

def surround_by_quote(a_list): 
    return ['"%s"' % an_element for an_element in a_list] 

env.filters["surround_by_quote"] = surround_by_quote 
0
# some.py file 
names = ['a', 'b', 'c'] 

# some.html file 
{{ names|safe }} 

# renders as the following, brackets included 
['a', 'b', 'c'] 
相关问题