Global variables - misonou/waterpipe GitHub Wiki

You can define global variables when calling waterpipe() or on the super global waterpipe.globals:

For an object path if the first segment is not a property name on the current object or any visible objects in the stack, it will be evaluated against the visible global objects.

// define global variables when calling `waterpipe()`
waterpipe(template, data, {
    globals: {
        myGlobal: 'foobar'
    }
});

// define variables on the super global `waterpipe.globals`
waterpipe.globals.myGlobal = 'foobar';

In either case the following template will output the string foobar if there is no myGlobal property in the input data object.

{{myGlobal}}  foobar

Note: Variables defined on waterpipe.globals will be shared across all template interpolation.

Defining global variables in template

Variables can also be declared within the template using as or let pipe function. They are also global in the sense that variables declared in one template are also visible for the template included.

For example the template innerTemplate included can read the variable tmpvar declared on the calling template.

waterpipe.pipes.innerTemplate = '{{tmpvar + 1}}';
{{let tmpvar 1}}
{{tmpvar}}  1
{{innerTemplate}}  2

Read-write scope

Although global variables are readable everywhere even in template inclusion, they are only overwritable within the template which they are declared.

waterpipe.pipes.innerTemplate = '{{tmpvar + 1 as tmpvar}}';
{{let tmpvar 1}}
{{tmpvar}}  1
{{innerTemplate}}  2
{{tmpvar}}  1
{{let tmpvar 3}}
{{tmpvar}}  3

Variables declared in the current global variable scope will mask out variable of the same name in outer scope. See Scope and name visibility for more information.

⚠️ **GitHub.com Fallback** ⚠️