Getting Started with Vipers

Release 1.0 - ...

Vipers are strong programming directives for retrieving and transforming data and creating logic. Vipers are logically collected in viper-classes. An example of such viper-class is the String class. Within these classed, there are viper methods. Viper-methods do have parameters. A simple example:

Smartsite SXML CopyCode image Copy Code
{string.toupper(hello world)}

Nesting

The incoming value of a viper parameter can be a viper as well, and therefore vipers can be nested:

Smartsite SXML CopyCode image Copy Code
{datetime.now()}
{datetime.getyear(datetime.now())}

Vipers can have more than one parameter. The next example uses the html.tag() viper-method to convert a string to an HTML H1 tag with it's innerhtml.

Smartsite SXML CopyCode image Copy Code
{html.tag(h1,hello world)}

The viper parameters are strongly typed. Most of the viper parameters do have datatype string:

Smartsite SXML CopyCode image Copy Code
{string.substring(hello world,6)}

Vipers can be nested by with a maximum of 50 nestings. Next example shows a welcome message based on the actual time using:

Smartsite SXML CopyCode image Copy Code
{sys.iif(
 datetime.gethours(datetime.now()) GTE 6 AND datetime.gethours(datetime.now()) LT 12, 
 'Good morning, this page is rendered at: ' + datetime.format(datetime.now(),'HH:mm:ss') , 
 sys.iif(
  datetime.gethours(datetime.now()) LT 18,
  'Goodday, this page is rendered at: ' + datetime.format(datetime.now(),'HH:mm:ss'),
  sys.iif(
   datetime.gethours(datetime.now()) LT 24,
   'Good evening, this page is rendered at: '+ datetime.format(datetime.now(),'HH:mm:ss'),
   'Goodnight, this page is rendered at: ' + datetime.format(datetime.now(),'HH:mm:ss')
  )
 )
)}

Quoting

Values being of datatype String, do not necessarilay be surrounded by quotes. Once the string is obtrusive you should. The string "hello world" of the previous example isn't obtrusive but it will be if it contains that can be interpreted as an operator. In this case, quotes (single or double) should be used:

Smartsite SXML CopyCode image Copy Code
{string.substring(hello world, how are you?,6)} => Error: Unknown operator '?'
{string.substring("hello world, how are you?",6)} => Ok: 'world, how are you?'
{string.substring('hello world, how are you?',6)} => Ok: 'world, how are you?'

If quotes within the string give problems this examples will give a solution:

Smartsite SXML CopyCode image Copy Code
{buffer.set(txt,"he's very "+'"lively"'+" today")}
{buffer.get(txt)}

{buffer.set(txt,'he'+"'"+'s very "lively" today')}
{buffer.get(txt)}

{buffer.set(txt,'he'+char.apostrophe()+'s very "lively" today')}
{buffer.get(txt)}

<se:buffer name="txt">he's very "lively" today</se:buffer>
{buffer.get(txt)}

Local vipers

Vipers are logically grouped into viper-classes. A special case on these viper-clases is the this class. Viper-methods in the this-class refer to the internal state of a macro. A common example is the this.result() viper-method. This method returns the result of a macro. This result can be used to format the result of a macro:

Smartsite SXML CopyCode image Copy Code
<se:itemdata field="title" resultformat="{html.tag(h1,this.result())}" />

Internal state refers also to valid executed macro:

Smartsite SXML CopyCode image Copy Code
<se:itemdata field="nonexistingfield" error="{this.error.smartsitecode()} - {this.error.type()} - {this.error.message()}" />