sipin / gorazor

Razor view engine for go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

string return value

mgutz opened this issue · comments

Is there a reason the return value on template functions is string instead of just bytes.Buffer? I understand it's more eye readable but it requires two transformations to write to ResponseWriter.

  1. The buffer is transformed to a string inside the template
return _buffer.String()
  1. Then the string is transformed to a []byte in a handler
    func index(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte(views.Index())
    }

Wouldn't this be more efficient

views.Index().writeTo(w)

@mgutz This this actually in the todo: https://github.com/sipin/gorazor#todo This very last line.

We are still in the process of turning gorazor's feature, like it's really needed to make all parameters in a layout to be string. We may start performance optimisation after "feature freeze". Nonetheless, PR are most welcomed.

:)

NP. I misunderstood that line. I'll try to find some time to do a PR. It might be better to return a SafeBuffer

type SafeBuffer {
    bytes.Buffer
    Safe bool
}

This is how template engines I've used in node.js determine whether a function's return value needs escaping.

You can see my fork. View the example. I don't think it's worth doing a PR as it will likely be rejected because it's more a refactoring and I want to remove even more from the library.