Skip to main content

Use if statements in DekiScript

This page applies to:MindTouch Responsive

If statements tell the computer to perform some action if a certain condition exists or to perform another action if some other condition exists.

 

If statements


To perform an action (as display a template) on a page if a certain condition exists.

Example

You might want to create an if statement to tell the computer what to do if a certain condition exists:

  1. Open a page in Edit mode.
  2. Copy the following code into a  DekiScript code block:
if(date.hours(date.now) >= 12) {
   template("Custom/Views/Header");
}
  1. Once you save the page, depending on the time of day, you may or may not see content:

 

If/else statements


  • To add information to a document that only specific users can see.
  • To add information to a document that only appears in certain circumstances. 
Example

You want to display text for admin users when they visit a page, but display a different text for non-admin users:

  1. Open a page in Edit mode.
  2. 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.";
}
  1. Once you save the page, depending on your viewing privileges, you will see one of the above texts:
If you do not want non-admin users to see anything, just do not type anything between the quotation marks.

 

Make sure that the "a" in Admin is capitalized.

If you would like DekiScript to ignore the case, create a variable for user.name and add string.tolowerbefore the variable as in the following example:

var luser = string.tolower(user.name);
if (luser == "admin")
{
    "Hello, admin.";
}
else
{
    "You are not an admin.";
}

 

If not statements


To exclude or change information on a page for specific users.

Example

You might want to create an if statement telling the computer what to do if something does NOT equal something:

  1. Open a page in Edit mode.
  2. Copy the following code into a  DekiScript code block:
var x =25;
if (user.name != "Admin")
{
let x = 75;
}
x;
  1. Once you save the page, depending on your viewing privileges, you will see one of the above texts:
75

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.