One base template is created. With that rendered first.html one more template.
eg. : var tmpl = template.Must(template.ParseFiles( "templates/base.html", "templates/first.html", ))
But I also want to add more .html files to render. Any reference?
When working with lightning web components there may be times when you want to render a component with different looks that are dependent on the data passed in. Instead of mixing all the different styles in one file, we can have multiple HTML templates in the same component and render them conditionally.
render() Render is mainly used to conditionally render a template. It defines the business logic to decide which template (HTML file) to use. In LWC this method doesn't do the actual rendering, but determines what template to use to render the component.
The power of Lightning Web Components is the templating system, which uses the virtual DOM to render components smartly and efficiently. It's a best practice to let LWC manipulate the DOM instead of writing JavaScript to do it. Use a <template> root tag in your HTML template.
If you define all your templates in a template-folder, you can easily parse the whole directory with:
template.Must(template.ParseGlob("YOURDIRECTORY/*"))
For example:
head.html
{{define "header"}} <head> <title>Index</title> </head> {{end}}
index.html
{{define "indexPage"}} <html> {{template "header"}} <body> <h1>Index</h1> </body> </html> {{end}}
main.go
package main import( "html/template" ) // compile all templates and cache them var templates = template.Must(template.ParseGlob("YOURTEMPLATEDIR/*")) func main(){ ... } func IndexHandler(w http.ResponseWriter, r *http.Request) { // you access the cached templates with the defined name, not the filename err := templates.ExecuteTemplate(w, "indexPage", nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
You execute your indexPage-Template with templates.ExecuteTemplate(w, "indexPage", nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With