I have hmtl template, and want to add some values there.
I was going to use sting.format module with such rule:
"Hello {name}! I am {years} years old".format(name = "Bob", years = "25").
So I have written such test
x = open(source_folder + "template.html", 'r')
template = x.read()
template.format(tester = "John")
studio_html = open(studio_folder + "/" + studio_name + ".html", 'w')
studio_html.write(template)
The beginning of the file is:
{tester}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1 /DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta ....
The full version of the file is here – http://bit.ly/R4zxQ6
I run the script, and it gives me errors, which tell that the html file contains other “{ }” containers… what can you suggest to do to avoid this problem?
Best regards, Sasha.
As has already been suggested, a real templating module would give you the best flexibility. However, it is possible to do it how you originally intended. To do so, you can escape the curly braces that should not be used for string formatting. For example, you would want to modify this:
as this:
When doubled/escaped like this, the format/vformat method will ignore them while parsing. Replacement fields should still have only one set of braces, e.g.
{tester}.