valyala / quicktemplate

Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template
MIT License
3.11k stars 150 forks source link

Disable HTML escaping #57

Open Ma124 opened 5 years ago

Ma124 commented 5 years ago

I really like QTPL but I rarely use it for HTML generation but rather go code generation. So I often write code like this to avoid the automatic HTML escaping which is really inconvenient:

func writeunesc(iow io.Writer, s string) {
    qw := qt.AcquireWriter(iow)
    streamunesc(qw, s)
    qt.ReleaseWriter(qw)
}

func unesc(s string) string {
    qb := qt.AcquireByteBuffer()
    writeunesc(qb, s)
    qs := string(qb.B)
    qt.ReleaseByteBuffer(qb)
    return qs
}

func streamunesc(qw *qt.Writer, s string) {
    qw.N().S(s)
}

to use it like this:

{% func Foo(param *ParamType) %}
Some
{%= unesc(param.Text) %}
stuff
{% endfunc %}

So I wanted to ask if there is already a way to deal with this and if not how one could implement this maybe as some thing like {%r param.Text %}

pierrre commented 1 year ago

You can already do it with {%s= params.Text %}.

However, I think it would be interesting to be able to disable escaping for a block of code.