If statements in DekiScript
- Applies to:
- All MindTouch Versions
- Role required:
- Draft Contributor
If statement example
Create an if statement to tell an application what to do if a certain condition exists:
- Open a page in Edit mode.
- Copy the following code into a DekiScript code block:
if(date.hours(date.now) >= 12) { template("Custom/Views/Header"); }
- Once you save the page, depending on the time of day, you may or may not see content.
Output: INTENTIONALLY NOT RENDERED
If/else statement examples
- Add information to a document that only specific users can see.
- Add information to a document that only appears in certain circumstances.
Display text for admin users when they visit a page, but display different text for non-admin users:
- Open a page in Edit mode.
- Copy the following code into a DekiScript code block:
if (user.name == "Admin") { "This information can only be seen by the admin. No other use user can see it."; } else { "If you do not want non-admin users to see anything, just do not type anything between the quotation marks."; }
- Once you save the page, depending on your viewing privileges, you will see one of the above texts:
Make sure that the "a" in Admin is capitalized.
For DekiScript to ignore case, create a variable for user.name
and add string.tolower
before the variable as in the following example:
var luser = string.tolower(user.name); if (luser == "admin") { "Hello, admin."; } else { "You are not an admin."; }
Evaluate a conditional expression, if the outcome of the expression is nil, false or 0, execute the block immediately following the if statement. Otherwise, execute the block following the optional else statement.
Different languages treat different values that are not a true/false boolean differently. In DekiScript false as well as nil and zero are logically false, while True and everything else (other than nil, false or 0) are treated as logically true.
To conditionally include a template:
if(date.hours(date.now) >= 12) { template("Custom/Views/Header"); }
Output: INTENTIONALLY NOT RENDERED
To conditionally emit one text or another:
if(user.anonymous) { "Welcome guest. Please register!" } else { "Welcome back ".. user.name .. "!" }
Output: Welcome guest. Please register!
If not statement example
- Exclude or change information on a page for specific users.
Telling an application what to do if something does NOT equal something:
- Open a page in Edit mode.
- Copy the following code into a DekiScript code block:
var x =25; if (user.name != "Admin") { let x = 75; } x;
- Once you save the page, depending on your viewing privileges, you will see one of the above texts:
In DekiScript false
as well as nil
and zero
are logically false, while true
and everything else (other than nil, false or 0) are treated as logically true.