-
-
Notifications
You must be signed in to change notification settings - Fork 7.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
Add template funcs namespaces #3042
Comments
I think it's way easier to have fewer functions, but with the namespaces to add more options. Thumbs up for this issue! |
I made a proof of concept locally that allows func stringsNS() *stringsNamespace {
// should probably use a global instead of a new instance
return &stringsNamespace{}
}
type stringsNamespace struct{}
// TrimPrefix returns s without the provided leading prefix string. If s doesn't
// start with prefix, s is returned unchanged.
func (_ *stringsNamespace) TrimPrefix(prefix, s interface{}) (string, error) {
p, err := cast.ToStringE(prefix)
if err != nil {
return "", err
}
ss, err := cast.ToStringE(s)
if err != nil {
return "", err
}
return strings.TrimPrefix(ss, p), nil
}
// funcMap := template.FuncMap{
// "strings": stringsNS, Usage:
So, we can accomplish what we want. We just need to come up with a more formal proposal to redo the funcmap with namespaces. |
Adding namespaces is a good idea if we add more functionality to the templates. The docs already loosely groups template functions by "namespaces" / usecases. However, do you want to put every function in a namespace, even standard ones like How do we handle this redundancy of template functions from a long-term perspective? Will we depecrate them (not immediately, but one day in the future) or do we continue with this double-barreled approach? Deprecating them would be affect nearly every Hugo site. While introducing namespaces, should we document the functions by namespaces and just mention that the aliases still exist? Or will it be done vice versa by leaving the docs as they are and note that the usage of namespaces if preferred/advised? |
I think we leave the once we have as aliases -- some of them may make sense to deprecate at some point, but some of them is obviously good as "short form" ( When we redo the docs, we might put emphasis on the "namespaced" versions.
|
Here is the suggested game plan:
interface TplFuncsNamespacer {
NewNamespace(d *deps.Deps) TplFuncsNameSpace // name + interface{} + global TemplateFuncs aliases
} Then
|
Related discussion: https://discuss.gohugo.io/t/5800 |
This commit moves almost all of the template functions into separate packages under tpl/ and adds a namespace framework. All changes should be backward compatible for end users, as all existing function names in the template funcMap are left intact. Seq and DoArithmatic have been moved out of the helpers package and into template namespaces. Most of the tests involved have been refactored, and many new tests have been written. There's still work to do, but this is a big improvement. I got a little overzealous and added some new functions along the way: - strings.Contains - strings.ContainsAny - strings.HasSuffix - strings.TrimPrefix - strings.TrimSuffix Documentation is forthcoming. Fixes gohugoio#3042
As a first step to remove the hard ties between `tplimpl` and the different namespace packages. The `lang` package is used as the first example use case. See gohugoio#3042
As a first step to remove the hard ties between `tplimpl` and the different namespace packages. The `lang` package is used as the first example use case. See #3042
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
See #3040
I don't mind adding more template funcs, but we should have some kind of name spacing to make them easier to manage/document, i.e.
strings.trimPrefix
etc. Not sure how the "." works in this case, but then find something else ("_"?)We should keep the "old" names as aliases, of course, but this will lead to more natural names like:
image.config
image.resize
@moorereason @digitalcraftsman
The text was updated successfully, but these errors were encountered: