Search code examples
pythontemplatesbottlests-simple-templatesystem

How to pass html directly to template


I want to pass HTML directly as a parameter in template(). I know I could do something like:

%for i in array:
  <a>{{i}}</a>
%end

but I need to pass it directly when I call template, I tried replacing &lt and &gt with < > with javascript but that did not work. I want to do this:

{{results_of_i_in_array}}

and the loop will occur in my main rather than in the template, I never found anyone asking the same question. Note: this question is NOT a duplicate of this question.

I am using bottle default templating system, thanks in advance.


Solution

  • Bottle doc:

    You can start the expression with an exclamation mark to disable escaping for that expression:

    >>> template('Hello {{name}}!', name='<b>World</b>')
    u'Hello &lt;b&gt;World&lt;/b&gt;!'
    >>> template('Hello {{!name}}!', name='<b>World</b>')
    u'Hello <b>World</b>!'