擅长:python、mysql、java
<p>如果您只想进行简单的字符串格式化,python包括<code>str.format()</code>方法和<a href="https://docs.python.org/2/library/string.html#template-strings" rel="nofollow">Template Strings</a>。在</p>
<h3>直接从文档</h3>
<pre><code>>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
...
ValueError: Invalid placeholder in string: line 1, col 11
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
...
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'
</code></pre>
<p>如果您需要在模板中引入任何类型的逻辑,那么我可以大胆地说,您将无法正确地编写自己的代码,并且您将希望使用库。<a href="http://jinja.pocoo.org/docs/" rel="nofollow">Jinja2</a>是一个很好的通用模板库。如果您曾经使用过Django的模板系统,那么使用Jinja2将非常容易,因为Jinja2是基于Django的。在</p>
<h3>我在Jinja2中使用的一些代码</h3>
^{pr2}$
<h3>在我的_模板.html</h3>
^{3}$
<p>Django模板和Jinja2模板之间的一个区别是Jinja2的理念不是让设计者更容易使用;因此,在模板中允许函数调用。在</p>