-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use jet template in iris ? #1281
Comments
Hello @wx85278161, Let's start by saying that you can use any template engine with Iris, i.e herotemplate or even quicktemplate, check the herotemplate example and quicktemplate engine, the tutorial on both sides tells you how to do it pretty easy, all you wan to know is that the Let's take for example the for standard net/http http.HandleFunc("/todo", func(w http.ResponseWriter, r *http.Request) {
view, err := views.GetTemplate("todos/show.jet")
if err != nil { /* handle error*/ }
id := r.URL.Query().Get("id")
todo, ok := todos[id]
if !ok {
http.Redirect(w, r, "/", http.StatusNotFound)
return
}
view.Execute(w, nil, todo)
}) let's convert it to iris, the only thing we have to change is the func (ctx iris.Context) {
view, err := views.GetTemplate("todos/show.jet")
if err != nil { /* handle error*/ }
id := ctx.URLParam("id") // == ctx.Request().URL.Query().Get("id")
todo, ok := todos[id]
if !ok {
ctx.Redirect("/", iris.StatusNotFound)
return
}
view.Execute(ctx.ResponseWriter(), nil, todo) // < -----
} Super simple, right? |
Thank you. I found that this jet template syntax works well, but some of its functions are not as useful as ctx. view. I look forward to iris having its own template. Thanks again. |
The idea of making all perfect is wrong, I know where we are good and where other tools exist that can be live side-by-side with Iris. Iris will never create its own template parser, we can't ignore the world arround us, there are ton of nice template parsers written in Go that are all compatible with In the end, it's not so hard to create functions for your own project, as shortcuts to work with However, because I think you are a new Gopher, If you tell me where Thanks. |
I forgot to mention it @wx85278161, but you can A custom view engine is any structure that completes the view.Engine (and optionally the view.EngineFuncer). However, I understand that, probably, you are not in the position to do it by yourself because you are afraid of mistakes, but give it a try - there is nothing better than learning something new in programming. Because I want to serve each Iris user, as much as I, biologically, can; I am thinking of adding built-in support for jet template parser. That way you will also learn how you can do it by yourself, this will give you a good taste of interfaces usecases in Go :) Sounds OK with you my friend? |
Thank you very much |
It's done @wx85278161. The commit you may want to read, to learn how I did it is that one: 82aa362#diff-57a98b135f39e03441272219ac18625b You will be able to use it when I make the
Have fun! |
Thank you for your answer and effort. |
…at: kataras#1281 Former-commit-id: 3e00bdfbf1f3998a1744c390c12fd70430ac0320
jet template
https://github.com/CloudyKit/jet
The text was updated successfully, but these errors were encountered: