iris icon indicating copy to clipboard operation
iris copied to clipboard

How does django AddFunc call context content

Open aadog opened this issue 2 years ago • 7 comments

How does django AddFunc call context content

tmpl.AddFunc("activeClass", func(routeName string) string {
            //get context url??
	return routeName
})

aadog avatar Dec 11 '22 19:12 aadog

Hello @aadog, there is not any http request information inside these type of template functions. You have to pass the information from the template code itself, e.g. activeClass("the route name here", document.request.uri) or whatever.

kataras avatar Dec 11 '22 19:12 kataras

Would it be better if we used it as below

	app.UseFunc(func(c iris.Context) {
		tmpl.AddFunc("print", fmt.Sprint)
		tmpl.AddFunc("printf", fmt.Sprintf)
		tmpl.AddFunc("println", fmt.Sprintln)
		tmpl.AddFunc("session", sess.Start(c))
		tmpl.AddFunc("ctx", c)
		tmpl.AddFunc("activeClass", func(routeName string) string {
			if c.RouteName() == routeName {
				return "active"
			}
			return ""
		})
		c.Next()
	})

aadog avatar Dec 12 '22 09:12 aadog

I think that's very wrong, we can't edit the template's functions for each incoming request!

Instead do this @aadog:

tmpl.AddFunc("activeClass", func(routeName string, uri string) string {
	return routeName
})
func handler(ctx iris.Context) {
  viewData := iris.Map {
     // pass information to the template so custom function's 'activeClass' second argument can be set.
     "URI": ctx.FullRequestURI(), 
  }
  ctx.View("templates/template.html", viewData)
}
{{ activeClass "your_route_name" .URI }}

kataras avatar Dec 12 '22 23:12 kataras

@kataras I don't think this is the right way,Sometimes it is worth sacrificing a little performance in exchange for rapid development. I hope ctx is built into the function table,

I'm developing sjr plugin for iris, jquery-ujs,Are you interested in doing it together?He will push iris to a new level

aadog avatar Dec 19 '22 12:12 aadog

awesomeProject1.zip

aadog avatar Dec 19 '22 12:12 aadog

awesomeProject2.zip

aadog avatar Dec 19 '22 13:12 aadog

@aadog it's very easy to add the ctx into the binding data, there is no need to be there for all projects because most of them will not use it directly.

app.Get("/functions", func(ctx iris.Context) {
  var functionsPage = struct {
	  // A function.
	  Now func() time.Time
	  // A struct field which contains methods.
	  Ctx iris.Context
  }{
	  Now: time.Now,
	  Ctx: ctx,
  }
  
  if err := ctx.View("functions.html", functionsPage); err != nil {
	  ctx.HTML("<h3>%s</h3>", err.Error())
	  return
  }
})
<!-- functions.html -->
<h1>Function: {{ Now }}</h1>
<h1>Field: {{ .Ctx.Request.URL.Path }} </h1>
<h1>Field Struct's Function (Method): {{ .Ctx.FullRequestURI }} </h1>

About the new project sjr plugin for iris, jquery-ujs you are talking about, of course I am in but the project files you uploaded look like it's on the very early stages, do you have a repository somewhere that I can navigate to?

kataras avatar Dec 24 '22 04:12 kataras