Search code examples
pythonhtmlpython-3.xpystache

Pystache html characters replaced when using multiple files


I am using pystache (on Python 3.4) to template HTML files to be sent through email. I have a main.mustache file in which some of the tags are meant to be replaced by content of other .mustache files. So I have something like this (simplified version):

main.mustache:

<body>
<table>
.......
{{some_params}}
....
</table>

{{special_warning}}
</body>

The {{special_warning}} tag is only used in some conditions and comes from file special_warning.mustache:

<table>
  <tbody>
  <tr>
    <td>
      <h4 style="margin: 0; margin-bottom: 20px;"
        Well, this is odd. please re-do last action.
      </h4>
    </td>
  </tr>
  </tbody>
</table>

In my python script I do:

special_message = ''
if <some condition>:
    special_message = renderer.render_path('special_warning.mustache', {})

proc_templ = renderer.render_path('main.mustache', {'special_warning': special_message , <the other params>})

The result I get is the correct message for the main.mustache part but the part coming from special_warning.mustache is HTML encoded:

<body>
<table>
.......
some_params
....
</table>

&lt;table&gt;
  &lt;tbody&gt;
  ....
  &lt;/tbody&gt;
&lt;/table&gt;

</body>

Any ideas how I can prevent this html encoding? Is it python string doing it, or the pystache rendering that does it?


Solution

  • Using triple brackets avoids html escaping. so my main.mustache should be:

    <body>
    <table>
    .......
    {{some_params}}
    ....
    </table>
    
    {{{special_warning}}}
    </body>