Navigation

Basic DI# Syntax

DI# language is a scripting language integrated into Dialog Insight that allows you to add dynamic and personalized content to messages and HTML templates. Before using DI# for conditions, calculations, or data manipulation, it's essential to understand its basic syntax—how the code is written and interpreted by the platform. This article presents the fundamental rules for integrating DI# code into content, including the use of tags, the structure of statements, and conventions to follow. It provides a necessary starting point before delving into more advanced concepts such as data types, conditional logic, or collections. By mastering these basics, you'll be able to write clear, valid, and maintainable DI# scripts.

Resources to go further


DI# Tags

When the compiler processes code, it looks for these opening and closing tags [[ and ]], indicating the boundaries of the code that needs to be parsed. Everything outside the DI# opening and closing tags is ignored. Inside those tags, if you want to display simple text that should not be interpreted as code, you can use the output.write(…); method. If there is only a single operation between the tags, you can use the [[=… ;]] shortcut. 

The following code [[="text";]] 
is a shortcut for [[output.write("text");]]

Escaping from HTML

Everything outside the opening/closing tags is ignored by the compiler, which allows DI# code with mixed contents. DI# code can be inserted in HTML documents to create templates, among other things. 

<p>This will be ignored by compiler</p>
[[="This will be analyzed by the DI# compiler";]]
<p>This will also be ignored by the compiler and displayed by the browser.</p>

OR

<p>This will be ignored by the compiler and displayed by the browser.</p>
[[output.write("This will be analyzed by the compiler");]]</p>
<p>This will also be ignored by the compiler and displayed by the browser.</p>
// Example 1 of advanced escaping using conditions
[[if(a > b){]]
This will be displayed if the expression is true.

[[} else {]]
    
Otherwise, this will be displayed.
[[}]] 

// Example 2 d'échappement avancé en utilisant des conditions
[[
if(a > b)
{
output.write(
"This will be displayed if the expression is true.");
}
else
{
output.write(
"Otherwise, this will be displayed.");
}
]]

In this example, the compiler will ignore blocks where the condition is not met, even if they're outside the DI# opening/closing tags. If the above condition is true, all the code contained in the else condition will not be evaluated since the DI# interpreter will skip the blocks where the condition is false. 


Instruction Delimiters

Like C#, Perl or PHP, the DI# language requires instructions to be terminated by a semicolon. The closing tags do not imply the end of a statement, the semicolon is mandatory. 


Comments

DI# allows 2 types of comments:

  1. Single-line comments: Single-line comments can be an entire line or just the last part of one. Begin your comment with // and everything for there to the end of the line will be ignored by the compiler. 
  2. Mutiline comments: Multiline comments can stretch over a large chunk of code. Begin your comment with /* and finish it with */ Everything in-between will be ignored by the compiler. 

Variable Scope

The scope of a variable depends on the context in which it has been declared. A variable defined at the code root will be visible for the entire script, and a variable declared in a function will be limited to that function. You can think of a variable's context as being delimited by { and }. Any variable declared inside curly brackets has a local scope. 

[[  
int x = 123; // global scope
string MyFunction(string value)
{
int y = 456; // local scope
x = x + 1; // global variables can be accessed in a function
}
]]

Functions

Declaring a Function
Function declaration in DI# follows the same basic structure as in C. 

[Type] [Name] ([Parameter1], [Parameter2], etc.)
{
Commands...
}

Functions must be defined at the root code level. They cannot be defined inside another function or inside any curly-bracket-delimited scope ({ }). 

/* Example of a simple function declaration, with 2 integer parameters and an integer
 return value:
*/  
int addNumbers (int x, int y)
{
return x + y;
}


// Example of function declaration with a default parameter value 
void OutputWithHTMLTag (string value, string Tag = "B")
{
if (value is null)
return;
Output.write("<" + Tag + ">" + value + ">");
}

Notes:

  • A function can return any data type (string, int, decimal, etc.), but if your function declaration specifies a return type, the function itself HAS to return a value of the specified data type no matter how it terminates its execution. 
  • The return type of a function can be void, in which case a simple return statement without any parameters is sufficient to end the function's execution, or reaching the end of the function's commands.   

Calling a Function
A function gets called like any other programming language:
functionName(params)     OR     functionName()

A void function (without a return value) can be used as a simple statement inside a function or any code block:
OutputWithTags("my text", "i"); 

Functions with a return value of any type other than void can be used as expressions or values:
int x = addNumbers(1,1);

Scope of variables within a function
Like it has been mentioned before, variables declared inside functions are visible only for that scope. Global variables declared at the root code level are visible everywhere. 


Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.