Viper Syntax
A viper is a lightweight but powerful syntax used to invoke Smartsite business logic.
Smartsite SXML | Copy Code |
---|---|
{site.name()} {sys.iif( 3 * 4 GT 10, 'equal to or more than 10' , 'less than 10')} {string.enclose(buffer.set(a, 12); $a + $a * $a, 'result: ')} |
The viper syntax has several advantages:
- Simple, easy-to-use.
The syntax for a viper is basically a mathematical function call enclosed within curly braces {}. - Powerful.
Nesting together with the expression evaluator allow for powerful constructs. - Editor neutral.
The viper syntax can be used in any -non XML- environment, even within wysiwyg editors. - Extendable.
You can extend the available vipers with a site by building viper modules or by adding translations. - Unobtrusive.
It is very unlikely that normal content includes a string that uses the viper syntax. To furher minimize the risk for collisions, unrecognized vipers are left untouched. - Intellisense.
Full intellisense support, including code completion is available for vipers.
Viper syntax
The complete definition of the viper syntax is as follows:
Viper component | Description | |
---|---|---|
{ | Viper starting left curly brace. | |
<ViperMethod name> | A name is between 3 and 80 characters. The first character must be in the range 'a-z'. The remaining characters must be in the rang 'a-z', '0-9', '.' or '_'. Note: all characters in the vipermethod name are lowercase! | |
( | Left parenthesis to mark the start of the parameter block. No whitespace is allowed between the left curly brace, viper method name and the left parenthesis. | |
[parameter [,parameter ...]] |
Parameters are optional. Multiple parameters must be separated using a comma. | |
)} | Viper closing sequence. No whitespace is allowed between these characters. |
Smartsite SXML | Copy Code |
---|---|
Not a viper (incorrect whitespace): { whitespace.notallowed () } Not a viper (incorrect casing): {MustBe.Lowercase()} Not a viper (must start with a-z): {_not_a_viper()} Not a viper (invalid characters): {not:a$viper()} |
Viper Parameters
The viper parameter block consists of zero or more parameters separated by a comma. The number of parameters depends on the viper method that is invoked. Many viper methods support overloads: several flavors of the same viper method with a different set of parameters.
Smartsite SXML | Copy Code |
---|---|
No parameter: {site.name()} One parameter: {string.quote('world')} Two parameters: {string.repeat('beh', 5)} |
Each viper parameter is an expression. Therefore, you can use any whitespace within parameters.
Smartsite SXML | Copy Code |
---|---|
Compact notation without whitepace: {string.replace('Hello World!','World','Universe')} More verbose notation spanning several lines: {string.replace( 'Hello World!' , 'World' , 'Universe' )} |
The viper parameter data cannot include the viper closing sequence. In this case, you must break the sequence using string concatenation.
Smartsite SXML | Copy Code |
---|---|
Invalid syntax: {string.trim(' {( invalid! )} ')} Valid syntax: {string.trim(' {( valid! )'+'} ')} |
You cannot use tags within viper parameters as this would break the viper over multiple elements. Instead escape the tags using entities (<, > or SXML escaping (char.lt(), char.gt()).
Smartsite SXML | Copy Code |
---|---|
Invalid syntax: {string.trim(' <br /> ')} Valid syntax: {string.trim(' <br /> ')} Valid syntax: {string.trim(' '+char.lt()+'br /'+char.gt()+' ')} |