You have been redirected from an outdated version of the article. Below is the content available on this topic. To view the old article click here.

StructrScript

StructrScript is the name of the internal scripting language that can be used in Template Expressions, Show Conditions and any of the attributes of HTML elements.

It is a simple interpreted scripting language that consists of keywords and functions. StructrScript has no control structures, it is interpreted like a fully functional language, although some of the Built-In Functions have side effects.

Examples

The following statements are typical examples for the use of StructrScript.

capitalize(page.name) Converts the first letter of the name of the current space to uppercase.
if(equal(page.name, 'index'), 'active', null) Outputs ‘active’ if the name of the current page is ‘index’, typically used in the class attribute of navigation elements to mark a link.
empty(me) Boolean expression to determine if the current request is made by an authenticated user, evaluates to false if no current user is set.
each(find('User'), print(data.name)) Outputs the name of each user in the system the current user has read access to.

Search results for "StructrScript"

StructrScript

A StructrScript context is explicitly defined by ${} and automatically assumed within auto-scripting contexts.

The return value of a StructrScript expression is determined by resolving the nested functions from the inside out.

The StructrScript mode can be used to test the behaviour of StructrScript expressions or perform very simple maintenance tasks.

StructrScript is the name of the internal scripting language that can be used in Content or Template nodes, Template Expressions, Show Conditions and HTML attributes. It is a simple interpreted functional scripting language that provides access to Structr’s functionality through a set of functions and keywords. StructrScript has no control structures, it is interpreted like a fully functional language, although some of the Built-In Functions have side effects.

Usage

If the value of a constant is an object, its contents can be dereferenced using dot notation.

Most keywords can be used in JavaScript and StructrScript contexts. In a StructrScript context, they can be used directly. In a JavaScript context the can be used via the $.get(<keyword>) function (or since release 3.5 also via $.<keyword>).

JavaScript-only keywords are made available by functions.

Editing scripts within Flow elements

To add functionality to the flow, we will add a small StructrScript snippet to query existing users in the database. Left-click the Script text box of the Action element to bring up a code editor and enter the script as shown below.

Visibility

For HTML elements, Structr offers two additional ways to control the visibility during page rendering: showConditions and hideConditions. Both can contain a StructrScript or Javascript expression and - if they are filled - are evaluated before rendering. Depending on the return value of the script, the current element and its substructure will be displayed or hidden. Both fields are auto-script environments, i.e. the text in those fields is interpreted as a script without the need for the template expression markers ${...}.

Log

Name Description
Prev Accepts another element’s Next socket
Next Connects to another element’s Prev socket
DataSource Accepts another element’s DataTarget
Script Given script will be executed as StructrScript with data in it’s context.

DataSource

Name Description
DataSource Accepts another element’s DataSource
DataTarget Connects to another element’s DataSource
Script Given script will be executed as StructrScript with data in it’s context.

Return

Name Description
Prev Accepts another element’s Next socket
DataSource Accepts another element’s DataSource
Script Given script will be executed as StructrScript with data in it’s context. If no script is given, Return will just use the given data as return value.

Supported Languages and Environments

Depending on the context, different kinds of Scripting environments and scopes are possible. In auto-scripting environments, Structr will automatically assume any code to be StructrScript, unless otherwise specified. Within this context, it’s possible to define other scripting environments by using a special notation similar to how explicit scripting contexts are defined. These special notation is further explained in the section for each specific scripting language.

Introduction

For JavaScript and StructrScript, the output on the console is the same as if you were executing the expression in a scripting context of a content element or template element.

Business Logic

Most of the processes in Structr are event-driven: things happen when database objects are created, modified or deleted. For these events, Structr provides lifecycle methods that you can implement on any type in the data model (even the built-in types), using either StructrScript or Javascript.

Introduction

The console supports five different modes that you can cycle through using Shift - Tab:

Template Expressions

In addition to JavaScript, a simplified script language called StructrScript is also supported, which is particularly suitable for short script expressions as well as simple text formatting and variable replacement.

Function Query

In the JavaScript context the same built-in functions are available as in the StructrScript context but a lot of convenient JavaScript functionality is available as well.

In the function repeater the complete contents of the text area are interpreted as a script. By default as StructrScript but by adding { before and } after the code, it will be interpreted as JavaScript. All options described in the scripting guide are available in this scripting context.

In the StructrScript context all built-in functions can be used to collect, filter, sort, etc. the data for the repeater.

MessageSubscriber

MessageSubscriber is a generic abstraction of a subscriber included in the Messaging-Engine Module module.
It can attach to any kind of MessageClient and will subscribe on that client with the specified topic.
If the client receives a message on given topic, a callback will be made to the subscriber and the message will be evaluated in a scripting context that is specified in the subscribers callback attribute.

The following properties can be configured for the subscriber:

Name Type Description
clients Array of MessageClient A list of all related clients that will be subscribed on.
topic String The topic that will be subscribed
callback String Specifies a script that is evaluated upon the reception of a relevant message.

Note: Instead of using a specific topic, it is also possible to use * as a wildcard subscription.
The default scripting context is StructrScript and the message’s content is injected as well and available via retrieve() under the following attributes.

Key Description
topic The topic the message was sent in.
message The message’s content.