Skip to content
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

Closed
aiweisir opened this issue Jun 19, 2019 · 7 comments
Closed

How to use jet template in iris ? #1281

aiweisir opened this issue Jun 19, 2019 · 7 comments

Comments

@aiweisir
Copy link

jet template
https://github.com/CloudyKit/jet

@kataras
Copy link
Owner

kataras commented Jun 19, 2019

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 iris.Context.ResponseWriter() is an http.ResponseWriter which is an io.Writer as well, so you can execute any template that accepts an io.Writer parameter.

Let's take for example the jet you posted above, there is an example on its repository: example/todos/main.go. Take look for example at this code block:

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 view.Execute(w, nil, todo) to -> view.Execute(ctx.ResponseWriter(), nil, todo):

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?

@aiweisir
Copy link
Author

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.

@kataras
Copy link
Owner

kataras commented Jun 21, 2019

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 net/http and iris. As an extra bonus Iris has built'n support for 6 different template parsers including the standard go html, what more can someone ask?

In the end, it's not so hard to create functions for your own project, as shortcuts to work with jet template engine.

However, because I think you are a new Gopher, If you tell me where ctx.View helps you that the jet template engine doesn't provide you tell me to post you down ton of different solutions and ready-to-use functions.

Thanks.

@kataras kataras closed this as completed Jun 21, 2019
@kataras
Copy link
Owner

kataras commented Jun 21, 2019

I forgot to mention it @wx85278161, but you can app.RegisterView custom view engines too, and use the ctx.View and any other features that iris gives to built'n view engines as well.

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?

@kataras kataras reopened this Jun 21, 2019
@aiweisir
Copy link
Author

Thank you very much

@kataras
Copy link
Owner

kataras commented Jun 22, 2019

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 v11.2.0 officialy available. Until then, you can take look at the two examples:

Have fun!

@kataras kataras added this to the v11.2.0 milestone Jun 22, 2019
@aiweisir
Copy link
Author

Thank you for your answer and effort.

@kataras kataras closed this as completed Jul 23, 2019
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
…at: kataras#1281

Former-commit-id: 3e00bdfbf1f3998a1744c390c12fd70430ac0320
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants