# handlebars [![Go Reference](https://pkg.go.dev/badge/github.com/flowchartsman/handlebars.svg)](https://pkg.go.dev/git.reinaldyrafli.com/aldy505/handlebars-go) This is yet another fork of [github.com/flowchartsman/handlebars](https://github.com/flowchartsman/handlebars). Handlebars for [golang](https://golang.org) with the same features as [handlebars.js](http://handlebarsjs.com) `3.0`. Hard fork of [Raymond](https://github.com/aymerick/raymond) to modularize and keep up with handlebars development. ![Handlebars Logo](https://github.com/flowchartsman/handlebars/blob/main/handlebars-gopher.png?raw=true "Handlebars") # Table of Contents - [Quick Start](#quick-start) - [Correct Usage](#correct-usage) - [Context](#context) - [HTML Escaping](#html-escaping) - [Helpers](#helpers) - [Template Helpers](#template-helpers) - [Built-In Helpers](#built-in-helpers) - [The `if` block helper](#the-if-block-helper) - [The `unless` block helper](#the-unless-block-helper) - [The `each` block helper](#the-each-block-helper) - [The `with` block helper](#the-with-block-helper) - [The `lookup` helper](#the-lookup-helper) - [The `log` helper](#the-log-helper) - [The `equal` helper](#the-equal-helper) - [Block Helpers](#block-helpers) - [Block Evaluation](#block-evaluation) - [Conditional](#conditional) - [Else Block Evaluation](#else-block-evaluation) - [Block Parameters](#block-parameters) - [Helper Parameters](#helper-parameters) - [Automatic conversion](#automatic-conversion) - [Options Argument](#options-argument) - [Context Values](#context-values) - [Helper Hash Arguments](#helper-hash-arguments) - [Private Data](#private-data) - [Utilites](#utilites) - [`Str()`](#str) - [`IsTrue()`](#istrue) - [Context Functions](#context-functions) - [Partials](#partials) - [Template Partials](#template-partials) - [Global Partials](#global-partials) - [Dynamic Partials](#dynamic-partials) - [Partial Contexts](#partial-contexts) - [Partial Parameters](#partial-parameters) - [Utility Functions](#utility-functions) - [Mustache](#mustache) - [Limitations](#limitations) - [Handlebars Lexer](#handlebars-lexer) - [Handlebars Parser](#handlebars-parser) - [Test](#test) - [References](#references) - [Others Implementations](#others-implementations) ## Quick Start $ go get git.reinaldyrafli.com/aldy505/handlebars-go The quick and dirty way of rendering a handlebars template: ```go package main import ( "fmt" "git.reinaldyrafli.com/aldy505/handlebars-go" ) func main() { tpl := `
Tags", "body": "
This is a post about <p> tags
", } tpl := handlebars.MustParse(source) result := tpl.MustExec(ctx) fmt.Print(result) ``` Output: ```htmlThis is a post about <p> tags
{{this}}
{{else}}No content
{{/each}} ``` When looping through items in `each`, you can optionally reference the current loop index via `{{@index}}`. ```html {{#each array}} {{@index}}: {{this}} {{/each}} ``` Additionally for map and struct instance iteration, `{{@key}}` references the current map key or struct field name: ```html {{#each map}} {{@key}}: {{this}} {{/each}} ``` The first and last steps of iteration are noted via the `@first` and `@last` variables. #### The `with` block helper You can shift the context for a section of a template by using the built-in `with` block helper. ```html{{name}}
{{else}}No content
{{/with}} ``` #### The `lookup` helper The `lookup` helper allows for dynamic parameter resolution using handlebars variables. ```html {{#each bar}} {{lookup ../foo @index}} {{/each}} ``` #### The `log` helper The `log` helper allows for logging while rendering a template. ```html {{log "Look at me!"}} ``` Note that the handlebars.js `@level` variable is not supported. #### The `equal` helper The `equal` helper renders a block if the string version of both arguments are equals. For example that template: ```html {{#equal foo "bar"}}foo is bar{{/equal}} {{#equal foo baz}}foo is the same as baz{{/equal}} {{#equal nb 0}}nothing{{/equal}} {{#equal nb 1}}there is one{{/equal}} {{#equal nb "1"}}everything is stringified before comparison{{/equal}} ``` With that context: ```go ctx := map[string]interface{}{ "foo": "bar", "baz": "bar", "nb": 1, } ``` Outputs: ```html foo is bar foo is the same as baz there is one everything is stringified before comparison ``` ### Block Helpers Block helpers make it possible to define custom iterators and other functionality that can invoke the passed block with a new context. #### Block Evaluation As an example, let's define a block helper that adds some markup to the wrapped text. ```html