Skip to content

Latest commit

 

History

History
60 lines (51 loc) · 1.1 KB

README.md

File metadata and controls

60 lines (51 loc) · 1.1 KB

v8tpl

Description

Using the library you can execute JavaScript templates in Go by V8 engine.

How to install

Download source:

go get -d github.com/sergei-svistunov/v8tpl/

Go to source directory:

cd $GOPATH/src/github.com/sergei-svistunov/v8tpl

Install depot_tools if you haven't it (https://www.chromium.org/developers/how-tos/install-depot-tools):

git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH=`pwd`/depot_tools:"$PATH"

Build and install the library:

make -j4 install

How to use

package main

import (
	"github.com/sergei-svistunov/v8tpl"
)

// The JS template's source must contain function `template(data)` that will be called to process data
const TEMPLATE = `
	var template = function(data) {
		return "Hello " + data.name;
	}
`

func main() {
	// Create template object
	tpl, err := v8tpl.NewTemplate(TEMPLATE)
	if err != nil {
		panic(err)
	}

	// Prepare data
	data := map[string]string{
		"name": "world",
	}

	// Process it
	res, err := tpl.Eval(data)
	if err != nil {
		panic(err)
	}

	println(res)
}