title: Advanced Python string formatting with custom placeholders?
body: <p>I have the following HTML template in a Python variable:</p>
  
  <pre><code>template = """
      &lt;script&gt;
          function work()
          {
              alert('bla');
          }
      &lt;/script&gt;
  
      Success rate is {value:.2%} percent.
  """
  </code></pre>
  
  <p>I want to substitute <code>{value:.2%}</code> with some decimal number with the appropriate formatting. Since my template may contain a lot of JavaScript code, I want to avoid escaping the curly braces with <code>{{</code> and <code>}}</code>, so using <code>template.format(...)</code> directly is not an option.</p>
  
  <p>Using <code>Template(template).substitute(...)</code> also seems impossible because I want advanced formatting for <code>value</code>.</p>
  
  <p>I could probably replace <code>{value:.2%}</code> with a corresponding <code>%(value)...</code> syntax and then use <code>template % ...</code>. But I'm not a fan of this syntax because most of the variables in a real-world template won't need advanced formatting, so I want to keep the placeholder syntax simple.</p>
  
  <p>So my question is, is it possible to use custom placeholders in the template that would allow for advanced formatting when necessary, i.e. to have simply <code>{{value}}</code> or <code>[[value]]</code> but also <code>{{value:.2%}}</code> or <code>[[value:.2%]]</code> ?</p>
  
  <p><strong>Edit:</strong> Finally, I want to avoid errors that <code>.format(...)</code> would produce when the template contains placeholders, such as <code>{{dont_want_this_substituted}}</code>, for which the passed dictionary doesn't contain a value. That is, I want to only substitute only certain placeholders, not all that appear in the template.</p>
  
  <p><strong>Edit 2:</strong> To achieve what I want, I can grab all placeholders with a regular expression first, then format their contents and finally make the replacement in the template. But I wonder if an easier solution exists.</p>
  
  <p><strong>Edit 3:</strong> It was suggested that I split the template to avoid issues with <code>format()</code>. I would like to avoid this, however, because the template actually comes from a file.</p>