Documentation ¶
Overview ¶
Package soyjs compiles Soy to javascript.
It fulfills the same interface as the javascript produced by the official Soy compiler and should work as a drop-in replacement. https://developers.google.com/closure/templates/docs/javascript_usage
It is presently alpha quality. See ../TODO for unimplemented features.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("file not found")
var Funcs = make(map[string]Func, len(funcs))
Funcs contains the available Soy functions. Callers may add custom functions to this map.
var PrintDirectives = map[string]PrintDirective{ "insertWordBreaks": {"soy.$$insertWordBreaks", true}, "changeNewlineToBr": {"soy.$$changeNewlineToBr", true}, "truncate": {"soy.$$truncate", false}, "id": {"", true}, "noAutoescape": {"", true}, "escapeHtml": {"soy.$$escapeHtml", true}, "escapeUri": {"soy.$$escapeUri", true}, "escapeJsString": {"soy.$$escapeJsString", true}, "bidiSpanWrap": {"soy.$$bidiSpanWrap", false}, "bidiUnicodeWrap": {"soy.$$bidiUnicodeWrap", false}, "json": {"JSON.stringify", true}, }
PrintDirectives are the builtin print directives. Callers may add their own print directives to this map.
Functions ¶
func ES6Identifier ¶
ES6Identifier creates an ES6 compatible function name without periods. It replaces all periods, which usually denominate namespaces in soy, with a double underscore. For example, from the file
{namespace say} {template .hello_world} Hello World {/template}
when ES6Identifier is called on
say.hello_world
it will return
say__hello_world
Types ¶
type ES5Formatter ¶
type ES5Formatter struct{}
ES5Formatter implements the JSFormatter interface and creates Javascript files following the ES5 Javascript format (without imports)
func (ES5Formatter) Call ¶
func (f ES5Formatter) Call(name string) (string, string)
Call returns two values, the name of the template to save in the called functions map, and a string that is written into the imports - for ES5, there are no imports
func (ES5Formatter) Directive ¶
func (f ES5Formatter) Directive(dir PrintDirective) string
Directive takes in a PrintDirective and returns a string that is written into the imports - for ES5, there are no imports
func (ES5Formatter) Function ¶
func (f ES5Formatter) Function(fn Func) string
Function takes in a Func and returns a string that is written into the imports - for ES5, there are no imports
type ES6Formatter ¶
type ES6Formatter struct{}
ES6Formatter implements the JSFormatter interface and creates Javascript files following the ES6 Javascript format (with imports)
func (ES6Formatter) Call ¶
func (f ES6Formatter) Call(name string) (string, string)
Call returns two values, the name of the template to save in the called functions map, and a string that is written into the imports
func (ES6Formatter) Directive ¶
func (f ES6Formatter) Directive(dir PrintDirective) string
Directive takes in a PrintDirective and returns a string that is written into the imports
func (ES6Formatter) Function ¶
func (f ES6Formatter) Function(fn Func) string
Function takes in a Func and returns a string that is written into the imports
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator provides an interface to a template registry capable of generating javascript to execute the embodied templates. The generated javascript requires lib/soyutils.js to already have been loaded.
func NewGenerator ¶
NewGenerator returns a new javascript generator capable of producing javascript for the templates contained in the given registry.
type JSFormatter ¶
type JSFormatter interface { // Template returns two values, the name of the template to save // in the defined functions map, and how the function should be defined. Template(name string) (string, string) // Call returns two values, the name of the template to save // in the called functions map, and a string that is written // into the imports Call(name string) (string, string) // Directive takes in a PrintDirective and returns a string // that is written into the imports Directive(PrintDirective) string // Function takes in a Func and returns a string // that is written into the imports Function(Func) string }
The JSFormatter interface allows for callers to choose which version of Javascript they would like soyjs to output. To maintain backwards compatibility, if no JSFormatter is specified in the Options, soyjs will default to the ES5Formatter implemented in exec.go
type JSWriter ¶
type JSWriter interface { // Write writes the given arguments into the generated javascript. It is // recommended to only pass strings and ast.Nodes to Write. Other types // are printed using their default string representation (fmt.Sprintf("%v")). Write(...interface{}) }
JSWriter is provided to functions to write to the generated javascript.
type Options ¶
type Options struct { Messages soymsg.Bundle Formatter JSFormatter }
Options for js source generation. When no Formatter is defined, soyjs will default to ES5Formatter from exec.go
type PrintDirective ¶
PrintDirective represents a transformation applied when printing a value.