字符串中的命名替换为 Mako

发布于 2024-09-26 12:56:29 字数 311 浏览 0 评论 0原文

在 Mako 中创建模板时,我需要编写如下内容: ${_('Hello, %(fname)s %(lname)s') % {'fname':'John','lname' :'Doe'}}

在编写该内容时,我不断收到 SyntaxException: (SyntaxError) 意外的 EOF while parsing 。有没有办法做同样的事情?

${_('Hello, %s %s') % ('John', 'Doe')} 有效,但在更改语言时不允许更改替换的顺序,如果需要。

谢谢。

When creating a template in Mako, I would need to write things like : ${_('Hello, %(fname)s %(lname)s') % {'fname':'John','lname':'Doe'}}

I keep getting SyntaxException: (SyntaxError) unexpected EOF while parsing when writing that. Is there wny way to do the same ?

${_('Hello, %s %s') % ('John', 'Doe')} works, but it does not allow to change the order of the replacements when changing language, if needed.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔少女心 2024-10-03 12:56:29

在 Mako 的 ${} 内部使用 {} 很复杂;显然 Mako 在找到第一个 } 后停止解析表达式。一种可能的解决方法是使用 dict() 而不是 {}

${_('Hello, %(fname)s %(lname)s') % dict(fname='John', lname='Doe')}

Using {} inside Mako's ${} is complicated; apparently Mako stops parsing the expression after finding the first }. A possible workaround is to use dict() instead of {}:

${_('Hello, %(fname)s %(lname)s') % dict(fname='John', lname='Doe')}
╰つ倒转 2024-10-03 12:56:29

尝试新的 Python 字符串格式:

>>> "{foo} {bar}".format(foo="foo", bar="bar")
'foo bar'
>>> "{foo} {bar}".format(**{"foo": "Hello", "bar": "World!"})
'Hello World!'

它看起来更好并且面向未来。

Try the new Python string formatting:

>>> "{foo} {bar}".format(foo="foo", bar="bar")
'foo bar'
>>> "{foo} {bar}".format(**{"foo": "Hello", "bar": "World!"})
'Hello World!'

It looks nicer and is futureproof.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文