CloudyKit / jet

Jet template engine
Apache License 2.0
1.24k stars 103 forks source link

data not resolved? #209

Closed marcelloh closed 10 months ago

marcelloh commented 10 months ago

When I call this:

Execute(w io.Writer, variables VarMap, data interface{})

I have an issue with this:

resolve(name string) (reflect.Value, error) 

Because I get an error: identifier "countrySelector" not available in current (map[field:country filter:NLD]) or parent scope, global, or default variables I know the data is filled with many parts, and countrySelector is one of them. For what I see is that resolve don't use the data which was put into st.context

(if data != nil {
   st.context = reflect.ValueOf(data)
})

Should I make data nil and put everything in variables (in the VarMap) ? (then why data exists?)

sauerbraten commented 10 months ago

It's difficult to help you without seeing your template and data, but in general, make sure you're accessing your context using a .. For example, if you pass e.g. a map[string]any{"foo": "bar"} as data, you would write {{ .foo }} to get bar into the output.

marcelloh commented 10 months ago

data = map[string]any inside it: countrySelector with a value of: interface {}([]datamodels.Country) [{ID: 1, Name: "Afghanistan", Code: "AFG", Region: "Asia", SubRegion: "Southern Asia"},*{ID: 2, Name: "Åland Islands", Code: "ALA", Region: "Europe", SubRegion: "Northern Europe"},+186 more] This is part of my template

{{ if field.Name() == "Country"}}
    {{ style = "width: 380px !important" }}
    {{ filter := .record["Country"] }}
    {{ field := "country" }}
    {{ collection = countrySelector}}
    {{ hasEmpty = "N" }}
    {{ include "common/selector.jet" }}
{{ else if field.Name() == "NativeLanguage"}} 
    {{ filter := .record["NativeLanguage"] }}
    {{ field := "nativelanguage" }}
    {{ collection = languageSelector}}
    {{ hasEmpty = "N" }}
    {{ include "common/selector.jet" }}
{{ else }}
    {{ field.Name() }} is not handled yet (in edit template)
{{end}}

This all worked in the master branch (without v6)

sauerbraten commented 10 months ago

I have no clue where you expect countrySelector in {{ collection = countrySelector}} to come from. If you access it like this, and don't declare and/or set it earlier, it would have to come from the VarMap. If it's in the context (data in the Execute() call), you would have to access it as .countrySelector.

Do you mean the code worked with v2? jet uses Go modules since its v3 release. The master branch is the v6.2.0 code.

marcelloh commented 10 months ago

I've changed from 2 to v6 and I was using . countrySelector, which should come from data. I investigated this a bit more. it seems that the error I've got when using . countrySelector is complaining about the fact that it isn't initialised. (So from v2 to v6 this has changed somehow) I've changed all the parts into :=

    {{ style = "width: 380px !important" }}
    {{ filter := .record["Country"] }}
    {{ field := "country" }}
    {{ collection := countrySelector}}
    {{ hasEmpty := "N" }}
    {{ include "common/selector.jet" }}

and now everything works again. Thanks for your patience.