Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Render multiple templates

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?

like image 585
fmt.Fprint Avatar asked Jun 20 '13 06:06

fmt.Fprint


People also ask

Can you use multiple HTML files in a lightning Web component?

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.

What is the use of render () in LWC?

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.

What is LWC template?

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.


Video Answer


1 Answers

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)

like image 108
ioboi Avatar answered Oct 12 '22 07:10

ioboi