-
Notifications
You must be signed in to change notification settings - Fork 249
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
config: No text/template wrapper #491
Conversation
you can do something like this: func (c *CLab) parseTemplates(pathToTemplates string) (*template.Template, error) {
t := template.New(c.Config.Name)
err := filepath.Walk(pathToTemplates, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".gotmpl") { // or any other condition identifying the template files
_, err = t.ParseFiles(path)
return err
})
return t, err
} The individual templates can be called with their name, which defaults to the file base name. t.ExecuteTemplate(os.Stdout, name, vars) you can get the list of templates associated with t.Templates() you can lookup a specific template associated with t.Lookup(name) |
Thanks @karimra. Changed to Not sure walking the root path is applicable. The idea here is to look for a specific file if you have a list of possible paths. |
It's still possible to parse the list of directories, and call the desired template by name. Parsing the whole directory, helps combining templates from different files as well. |
If we need templates from multiple files that is a requirement I did not have in mind. Do you see this as a requirement? It seems quite ineffective, because of the possible amount of templates in a folder. You will end up loading all templates, for all kinds, over all folders specified. If that is indeed the case, we should just run
Maybe with globs we could keep some of the existing logic, but only load all templates for the active role/kind |
Templates can be combined by importing each other. In that case you would need to load the main template as well as the ones it imports. Separating templates in multiples files allows reuse and separation of responsibilities/concerns, it's really at the core of the template engine design. I don't think this has to be addressed in this PR, since we didn't really come to a conclusion concerning how templates will be mapped to kinds and nodes. Can explain how a template file is mapped to a node in this PR? Is it a file name that gets inherited from kinds/defaults like the other variables? |
You can pass a list of templates names. The template names will be combined with the role: By default the role is the kind, except if you specify it in vars. |
yes, the idea here is that the template name is not related to a single node, but to a group of them. The default group marker is its So for a lab that has |
I would phrase the group of them slightly differently:
Any finer control can be done inside the template with if constructs using even more of the vars on the node/link level. |
Indeed the
|
it seems all this assumes that templates are being run with separate command, not run on deploy ? |
Indeed, config only deploys to running labs. try:
add -c 100 to view the rendered templates if you dont have an active lab |
ok, if this is the case currently, |
@karimra do you have anything else you had in mind for this PR? |
it's still hard for me to see the whole picture, go ahead. once the other PR is done it will hopefully be clearer. |
I'll make some slides to capture the whole picture, I've been asked to present it to an internal community as well. This should also give a start for the site/docs. |
I get how it works and how to use it, what I don't get is the Go code. |
Nr 1 from #487
Implementation similar to the existing logic from https://github.com/kellerza/template/blob/main/template.go#L59
@karimra mentioned there is a built in method to search the paths, but did not find it.