Allow Generic types for Metas
I tried the following:
type RichText string
func (RichText) ConfigureQorMeta(metaor resource.Metaor) {
if meta, ok := metaor.(*admin.Meta); ok {
meta.Type = "rich_editor"
meta.Config = &admin.RichEditorConfig{}
}
}
type Post struct {
gorm.Model
Title string
Body RichText
}
But the temple func "raw" expects string, e gives me an error.
"raw": func(str string) template.HTML { return template.HTML(utils.HTMLSanitizer.Sanitize(str)) },
This looks like something I wanted to try next week to unify a lot of repepritive code in our product.
I belive this shoudl work somehow.. Can you share the full error and stacktrace? Or: in which template does it trip over {{ raw ... }} ?
Here: https://github.com/qor/admin/blob/master/views/metas/form/rich_editor.tmpl
hmm... that calls it with .Value. I'm not sure what that is in your case but to debug further I'd try to change the "raw" template func to something like this and see.. Maybe you need to implement stringer on your type or need to case the string out of your custom implementation.
func raw(iv interface{}) (template.HTML, error) {
log.Println("debug raw value:", iv)
switch v := iv.(type) {
case string:
return utils.HTMLSanitizer.Sanitize(v), nil
default:
return "", fmt.Errorf("raw: unhandled type: %T", iv)
}
}
Thanks for your reply and attention. This specific problem its easy to solve. I opened this issue with a more generic intention. I want to implement not only a type for rich editor, but for other commom types too. See my other issue #151. For example:
These types woul configure the options
type Gender string
type Country string
These types would replace the meta template(add a unity selection on the right of the value, allowing for switching between meter and centimeter for example, and automatically convert the value of the input), valuer(append Unity, default(which can be meter as well) for Distance and meter for Meter) and setter(trim Unity from string),
type Distance uint64
type Meter Distance
Well, not a beautiful solution, but works:
type RichText string
func (RichText) ConfigureQorMeta(metaor resource.Metaor) {
if meta, ok := metaor.(*admin.Meta); ok {
meta.Type = "rich_editor"
meta.Config = &admin.RichEditorConfig{}
meta.GetBaseResource().(*admin.Resource).GetAdmin().RegisterFuncMap("raw", func(str interface{}) template.HTML {
return template.HTML(utils.HTMLSanitizer.Sanitize(utils.ToString(str)))
},
)
}
}
type Post struct {
gorm.Model
Title string
Body RichText
}