Skip to content

Add a configurable template engine

Hossein Pursultani requested to merge 19-template-engine into main

What does this MR do?

The new Template type provides a configurable wrapper around the standard text/template package to parse and render a group of inter-related text templates.

Additional interfaces provide a discovery and load mechanism with a default implementation from fs.FS source type.

How to test

  1. Create the template directory and sample template files:

    mkdir -p assets/templates/test-1/{.ignore,partials} 
    touch assets/templates/test-1/{a,b,.x,partials/_p}.txt
  2. The following code snippet renders a.txt and b.txt:

     func main() {
     	tpl := template.GetTemplate("test-1")
     	if tpl == nil {
     		panic("test-1 must exist")
     	}
    
     	data := map[string]any{
     		"A": map[string]any{
     			"A1": "a1",
     			"A2": "a2",
     		},
     	}
    
     	result, err := tpl.Render(data)
     	if err != nil {
     		panic(err)
     	}
    
     	fmt.Println(result)
     }

Follow up

Once this MR is merged, the next step is to deserialized Kubernetes objects from parsed templates.

Author's Checklist

For anything in this list which will not be completed, please provide a reason in the MR discussion.

Required

  • Ensure a release milestone is set.
  • MR title and description are up to date, accurate, and descriptive.
  • MR targeting the appropriate branch.
  • MR has a green pipeline on GitLab.com.
  • When ready for review, MR is labeled workflowready for review per the MR workflow.

Expected

  • Test plan indicating conditions for success has been posted and passes.
  • Documentation is created or updated.
  • Tests are added.

Related issues

Related to #19 (closed)

Edited by Hossein Pursultani

Merge request reports