【问题标题】:The ${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}} syntax doesn't work in a Mako template${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}} 语法在 Mako 模板中不起作用
【发布时间】:2011-01-14 09:09:09
【问题描述】:
在Mako 模板中,我需要执行以下操作:
${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}}
当 A 这样做时,我收到此错误:
SyntaxException: (SyntaxError) 解析时出现意外 EOF
(, line 1) ("'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'") 在文件中...
我该如何解决这个问题?
我需要在翻译文本中使用这种语法:
$(_(u'foo bar %(a)s ... %(b)s) % { ... })
【问题讨论】:
标签:
python
templates
mako
【解决方案1】:
解决方法是以不同的方式传递 dict 对象。例如:
from mako.template import Template
print Template("${'foo %(a)s bar %(b)s' % data}").render(data=dict(a='Alpha',b='Beta'))
【解决方案2】:
解决方案:
${'foo %(a)s bar %(b)s' % dict((('a', '1'), ('b', '2'),))}