Golang html/template.Template.Clone function example
package html/template
Clone returns a duplicate of the template, including all associated templates. The actual representation is not copied, but the name space of associated templates is, so further calls to Parse in the copy will add templates to the copy but not to the original. Clone can be used to prepare common templates and use them with variant definitions for other templates by adding the variants after the clone is made.
It returns an error if t has already been executed.
Golang html/template.Template.Clone function usage example
var baseSrc = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.PageTitle}}</title>
<!-- Bootstrap -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>{{.PageH1}}</h1>
{{template "main" .}}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>`
var templates = make(map[string]*template.Template)
baseTemplate := template.Must(template.New("base").Parse(baseSrc))
newBase := template.Must(baseTemplate.Clone())
References :
Advertisement
Something interesting
Tutorials
+6.5k Grep : How to grep for strings inside binary data
+11.9k Golang : Determine if time variables have same calendar day
+10.1k Golang : How to tokenize source code with text/scanner package?
+11k Golang : Create Temporary File
+4.7k Javascript : Access JSON data example
+7.3k Golang : How to fix html/template : "somefile" is undefined error?
+19.1k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+7.1k Restart Apache or Nginx web server without password prompt
+9.5k Mac OSX : Get a process/daemon status information
+5.6k PHP : Convert CSV to JSON with YQL example
+4.9k JQuery : Calling a function inside Jquery(document) block
+6.3k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?