私はテンプレートとして使用されるxmlファイルを持っています。 このxmlの中に実際のデータに置き換えられる複数のマーカーがあります。これは私がやったことです:
複数のテキスト置換マーカーによる文字列
def populate_template(self, value1, value2, value3):
with open('my_template.xml', 'rb') as xml_template:
template_string = xml_template.read()
template_string.replace('{{MARKER_1}}', value1)
template_string.replace('{{MARKER_2}}', value2)
template_string.replace('{{MARKER_3}}', value3)
return template_string
各マーカーは、テンプレート内で複数回出現することができます。
これを行うより効率的な方法があるのだろうかと思っていましたか?
テックのもの:
- のPython 2.7
ありがとうございました。あなたのコードを 'Environment#get_template'メソッドでテンプレートを取得するように変更しました。ありがとう! –