golang template
TRANSCRIPT
- 1. GOLANG TEMPLATEGOLANG TEMPLATE By, Karthick Kumar V
- 2. Golang an OverviewGolang an Overview Go is a compiled language Its syntax loosely derived from C It is a fast-compiled, garbage-collected, concurrent systems programming language It can compile a large project within a few seconds It is designed for multi-core computers
- 3. Template an OverviewTemplate an Overview Templates are a way to merge generic text with more specific text Example : ERB / Haml Template Engine for Ruby
- 4. Templating in GolangTemplating in Golang In Go Template Package is to handle Templating To rendering the output It has 2 types of Template Package 1. Text Template - import text/template 2. HTML Template - import html/template
- 5. Text Template PackageText Template Package Package Text template implements data-driven templates for generating textual output It will not support generating HTML outputs Execution of the template is represented by a period '.' and called "dot"
- 6. HTML Template PackageHTML Template Package Package HTML template implements data-driven templates for generating HTML output safe against code injection. It provides the same interface as package text/template This package wraps package text/template so you can share its template API to parse and execute HTML templates safely
- 7. Basic Syntax of TemplatingBasic Syntax of Templating Field
substitution - {{.FieldName}} - To include the content of a field
within a template - It is enclose within curly braces and add a dot
at the beginning - Example :
Hello, {{.Text}}
- It is similar to writing ERB in Ruby - Example :Hello,
- 8. Basic Syntax of TemplatingBasic Syntax of Templating Nested fields - {{range}} and {{with}} - Printing the output in Loop - {{range}} just like range in Go - {{with}} lets you write the same object name once and use {{.}} as shorthand Pipeline - {{pipeline}} - The default textual representation of the value of the pipeline is copied to the output. Ex: {{. | html}}
- 9. Basic Syntax of TemplatingBasic Syntax of Templating Conditions {{if}},{{else if}} and {{else}} - To check for conditions in templates - We can use the if-else syntax just like you do in regular Go programs - We cannot use conditional expressions in if, for instance - Example : {{.Mail}}=="[email protected]" - Only boolean values are acceptable
- 10. Basic Syntax of TemplatingBasic Syntax of Templating Template variables - {{$variable}} - To use local variables in templates - We can use them with the with, range and if keywords - their scope is between these keywords and {{end}} - Example : {{with $x := "output" | printf "%q"}}{{$x}}{{end}}
- 11. Basic Syntax of TemplatingBasic Syntax of Templating Nested templates - Code Reusability - The templates can be reused across other templates - like headers and footers of a blog - We can declare header, content and footer as sub- templates - Example : {{define "sub-template"}}content{{end}}
- 12. Basic Syntax of TemplatingBasic Syntax of Templating Must - function - The template package has a function called Must - It is used or validating templates, like the matching of braces, comments, and variables - Example : template.Must(template.ParseFiles("index_temp.html")
- 13. Methods in TemplatingMethods in Templating To load the Template from a string or file and then perform the merge 1. Parse 2. ParseFile 3. Execute 4. ExecuteTemplate 5. os.Stdout
- 14. Methods DescriptionMethods Description Parse - It is used to render textual data - Its is used in text/template ParseFile - It is used to render HTML files - It is used in html/template
- 15. Methods DescriptionMethods Description Execute - It is used to display textual output - Its is used in text/template ExecuteTemplate - It is used to load HTML output - It is used in html/template os.Stdout - It is a standard output to print out the merged data
- 16. SummarySummary To combine dynamic data with templates using techniques including printing data in loops, template functions and nested templates The input text for a template is UTF-8-encoded text in any format. "Actions" data evaluations or control structures are delimited by "{{" and "}}"; all text outside actions is copied to the output unchanged.