emmett-framework / renoir

A templating engine designed with simplicity in mind
BSD 3-Clause "New" or "Revised" License
37 stars 3 forks source link

How to output HTML from a function in a template #2

Closed denesl closed 3 years ago

denesl commented 3 years ago

Hi

what is the Renoir syntax to output an unescaped HTML string created by a function in a template? If I have {{=f()}} in the template then I get an escaped string e.g. <div class="some-class" ... obviously I want <div class="some-class" ...

Thanks.

gi0baro commented 3 years ago

@denesl Renoir uses an __html__ method on objects for escaping when available. You can mimic what Emmett does building a wrapper class:

class asis:
    def __init__(self, val):
        self.val = val

    def __html__(self):
        return str(self.val)

the you function just have to return the wrapped content

def f():
    ...
    return asis(whatever)

or you can add you class in Renoir's context and call-it directly from the template:

{{ =asis(f()) }}