Squidex
  • Welcome to Squidex
  • Getting Started
    • Squidex Cloud
    • Installation Instructions
      • Platforms
        • Install on Kubernetes
        • Install on AWS (Deprecated)
        • Install on AWS EC2 using Docker
        • Install on Azure
        • Install on Azure using ARM
        • Install on Google Cloud Platform (GCP)
        • Install on Docker
        • Install on Heroku
        • Install on IIS
        • Install on Render
        • Install on Vultr
      • Configuration
        • Deploying the Image Resizer Service
      • Troubleshooting and Support
        • Diagnose Runtime Issues
        • Restoring Deleted Apps
      • Install Identity (Deprecated)
      • External Identity Providers
        • Azure AD (OAuth) with Squidex
    • Contributing and Developing
      • Building
      • Developing
      • Extensions
        • Custom Rule Action
      • Contributing
      • Architecture
      • Translating
      • Squidex Docs Program
    • Roadmap
    • Quick Start Guides
      • Vue.js Blog with Squidex
      • React.js Blog with Squidex
      • Angular Blog with Squidex
  • Documentation
    • Introduction and Use Case
    • Concepts
      • Apps
      • Teams
      • Schemas
        • Field Rules
      • Content
        • Scheduled Publishing
      • Assets
        • Querying Assets
      • Localization
      • Migrations
      • Roles & Permissions
      • Rules
        • Publish an Event to Azure Queues using Rules
        • Populate Elasticsearch Index
      • Backups
      • Subscriptions
        • App Subscriptions v/s Team Subscriptions
      • Notifications
      • Dashboard
      • Workflows
    • Software Development Kits
      • TypeScript
      • .NET
        • Version v14 (and Earlier)
        • Version v15 (and Later)
      • PHP
      • Java
    • Developer Guides
      • API
        • Authentication
        • Postman
        • Queries
        • Assets
      • Automation Tools (CLI)
      • Scripting
        • Scripting Helper Methods
      • Embed Content
      • Custom Workflows
      • Custom Editors
      • Custom Sidebars
      • Preview Content
      • Rule Formatting
        • Simple
        • Script
        • Liquid
      • Tutorials
        • Building a Blog with Squidex and Next.js
  • Next
    • Squidex 3.0: API Compatibility
Powered by GitBook
On this page
  • Basic Syntax
  • Special Functions
  • Examples
  • Resolve References
  • Resolve References
  • Conditional Formatting

Was this helpful?

  1. Documentation
  2. Developer Guides
  3. Rule Formatting

Script

How to Format Rules with JavaScript Expressions

PreviousSimpleNextLiquid

Last updated 1 year ago

Was this helpful?

JavaScript expressions can be used with the following syntax:

Script(<YOUR_SCRIPT>)

In newer versions of Squidex, the user interface has been improved and custom input fields have been introduced which allows selection of the syntax and adds the necessary prefix automatically.

Basic Syntax

The scripting engine supports almost all ES6 features with a .

Therefore, it is best to use the syntax and just reference properties directly:

Script(`${event.appId.id}`)
Script(`${event.appId.Name}`)
Script(`${event.user.id}`)
Script(`${event.user.email}`)

// For content events
Script(`${event.schemaId.id}`)
Script(`${event.schemaId.Name}`)
Script(`${contentUrl()}`)
Script(`${contentAction()}`)
Script(`${event.data.city.de}`)

Special Functions

Squidex provides a set of general helper functions for scripting and rule formatting.

A value list can be found in the documentation concerning scripting helper methods here:

Additionally, there are also methods which are only available for rule formatting.

Name
Description

contentAction()

The status of the content, when the event is a content event.

Otherwise null.

contentUrl()

The URL to the content in the Management UI, when the event is a content event. Otherwise null.

assetContentUrl

The URL to download the asset, when the event is an asset event. Otherwise null. This URL does not include the app name and is therefore not recommended.

assetContentAppUrl

The URL to download the asset by ID, when the event is an asset event.

Otherwise null.

assetContentSlugUrl

The URL to download the asset by slug, when the event is an asset event.

Otherwise null.

complete(value)

If you use an asynchronous operation, just like getAssets you have to tell the script engine, which value should be returned. Therefore you have call complete(value) with the result value. If you do not call this method, the result of the last statement is used.

Examples

Resolve References

You can use scripting to resolve references. You must pass over an array of content IDs and a callback (that is invoked) with the resulting list of content items.

Script(
    getReferences(data.references.iv, function (references) {
        var actual1 = `Text: ${references[0].data.field1.iv} ${references[0].data.field2.iv}`;
        var actual2 = `Text: ${references[1].data.field1.iv} ${references[1].data.field2.iv}`;

        complete(`${actual1}\n${actual2}`);
    });
)

Or a single reference:

Script(
    getReference(data.references.iv[0], function (references) {
        var actual1 = `Text: ${references[0].data.field1.iv} ${references[0].data.field2.iv}`;

        complete(`${actual1}`);
    })
)

Resolve References

You can use scripting to resolve assets. You have to pass over an array of asset IDs and a callback (that is invoked) with the resulting list of assets.

Script(
    getAssets(data.assets.iv, function (assets) {
        var actual1 = `Text: ${assets[0].fileName} ${assets[0].id}`;
        var actual2 = `Text: ${assets[1].fileName} ${assets[1].id}`;

        complete(`${actual1}\n${actual2}`);
    });
)

Or a single asset:

Script(
    getAsset(data.assets.iv[0], function (assets) {
        var actual1 = `Text: ${assets[0].fileName} ${assets[0].id}`;
        
        complete(`${actual1}`);
    });
)

Conditional Formatting

You can use if-statements and other JavaScript language features for conditional formatting.

In the following example, different payloads have been created, depending on the asset size:

Script(
    if (event.fileSize > 100000) {
        return `I just uploaded a large image ${event.fileName}`;
    } else {
        return `I just uploaded a small image ${event.fileName}`;
    }
)
few restrictions
Javascript template string
Scripting Helper Methods