Sahi Pro - Include APIs

To reuse a function from one script in another, we include the relevant script using the include APIs.
You may also like to see Including Sahi script globally

_include

Since Sahi Pro: 5.0
Since Sahi OS: NA

_include($scriptPath)

Arguments
$scriptPathstring File path of sahi script.
If scriptPath is a relative path, it is evaluated relative to including scripts's path

Details

Includes the script at scriptPath in the current script.
_include is dynamically evaluated since Sahi Pro V5.0.

// Suppose the current script is in
// D:/sahi/userdata/scripts/demo/ folder

// include lib.sah located in the current script's directory
// D:/sahi/userdata/scripts/demo/lib.sah
_include("lib.sah"); // using relative path

// Include using full path
_include("D:/sahi/userdata/scripts/demo/lib.sah"); // using absolute path

// Use of back and front slashes
_include("D:\sahi\userdata\scripts\demo\lib.sah"); // WRONG
_include("D:/sahi/userdata/scripts/demo/lib.sah"); // CORRECT
_include("D:\\sahi\\userdata\\scripts\\demo\\lib.sah"); // CORRECT

// Include from a subfolder
// D:/sahi/userdata/scripts/demo/common/lib.sah
_include("common/lib.sah");

// Include from a parent's sub folder
// D:/sahi/userdata/scripts/common/lib.sah
_include("../common/lib.sah");

// Using a variable
var $includePath = "../common/lib.sah";
_include($includePath);

_includeOnce

Since Sahi Pro: 5.0
Since Sahi OS: NA

_includeOnce($scriptPath)

Arguments
$scriptPathstring File path of sahi script.
If scriptPath is a relative path, it is evaluated relative to including scripts's path

Details

Similar to _include but _includeOnce only includes the script once even when called multiple times.

This is useful in a scenario where during development a single flow is broken
into multiple scripts and each is developed and tested independently and then
included all together into one script.

_includeOnce("lib.sah"); // includes lib.sah
_includeOnce("lib.sah"); // second call. Will do nothing.

// For more details on usage, look at _include

_dynamicInclude

Since Sahi Pro: 3.5
Since Sahi OS: 3.5

_dynamicInclude($scriptPath)

Arguments
$scriptPathstring File path of sahi script.
If scriptPath is a relative path, it is evaluated relative to including scripts's path

Details

dangerDeprecated: Same as _include since V5.0. Use _include instead.