Serverside JavaScript

Search
Feedback

To enable more complex scripting the Rhino serverside JavaScript engine (see Rhino (Wikipedia)) has been integrated into Structr.

The template expression to inform the parser that we want to use JavaScript is ${{}} (double curly brackets).

Note: In places where it is not necessary to surround StructrScript code with ${} (e.g. Hide and show conditions, Function Queries, Custom Schema Methods) because it is automatically inserted we surround our code with {} to be able to use JavaScript.

Functions

All built-in functions are made available via the global Structr object. They can be accessed by their regular name or by using the CamelCased name.

Example:

${{
	Structr.send_plaintext_mail( fromAddress, fromName, toAddress, toName, subject, content );

	/* is equivalent to */

	Structr.sendPlaintextMail( fromAddress, fromName, toAddress, toName, subject, content );
}}

Note: Not all of the built-in functions are necessary in JavaScript. For example the arithmetic functions add, sub and the like are available but should not be used as there are easier ways to do that in JavaScript.

Keywords

The basic StructrScript Keywords are also available in JavaScript - the way they are accessed is different though. We need to use the get(keyword) function on the global Structr object. The only exceptions are the me and this keywords which are accessible via the shortcut Structr.me and Structr.this.

${{
	var obj = Structr.this;
	var me = Structr.me;
	var current = Structr.get("current");
	var request = Structr.get("request");
}}

Parameters for custom methods

Custom schema methods can accept parameters. There are two ways of accessing these parameters. (Note that we are omitting the opening ${ and closing })

  1. If we are only interested in a single parameter we can use the following code to retrieve it.

    {
    	var singleParam = Structr.retrieve('parameterName');
    }
    
  2. If we want to retrieve all the parameters there is a special Structr.vars object which contains all parameters as a map indexed by the parameter name.

    Example:

    Calling a method with the parameters “param1” = “Test” and “param2” = 12 would result in the following:

    {
    	var singleParam = Structr.vars;
    	/*
    	singleParam now contains the object
    	{
    		param1: "Test",
    		param2: 12
    	}
    	*/
    }
    

Caution

The Rhino engine is pretty unforgiving when it comes to null values and the node rendering exits when such an error occurs. That is why there are many null-checks throughout serverside JavaScript code.

Graph-Browser

About this article
Last change 2016-03-04
Topics FrontendServerStructr 2.0