Search code examples
pythonhtmlxmlodoomako

Html string from a variable not rendered using mako in python


When I try to render a string variable in mako template like: ${ variable_name } As, the variable contains html content, the content is not rendered properly. Rather than displaying HTML, the output just displays the source code like:

<div>...<p>..</p>...</div>

But the HTML directly written in MAKO renders correctly. Imean

var = <p>Not Rendering HTML</p>
Line 1:  <p>Testing line</p>
Line 2:  ${var}

Line1 renders as: Testing line

But line 2 renders as: <p>Not Rendering HTML</p>

What should I do...?


Solution

  • Try outputting your variable with a n filter, like so:

    ${var | n}
    

    This should disable all default filtering. You can read more about filtering here: http://docs.makotemplates.org/en/latest/filtering.html

    You may also want to look at this question and its answers: Mark string as safe in Mako