Teya Salat
/ /

XtScript: Print

Learn how to output your XtScript Codes to your site

print is the command to output a variable or string. It is similar to document.write() in Javascript or echo in PHP.

But unlike in Javascript and PHP, there are many things need special attention when using xtscript print command.

According to syntax.xtgem.com, print command in xtscript have rules:
  • print cannot be used directly with functions
    You need to assign the function to a variable and then print the variable
    Although if you call a funtion direct the result is printed anyway
  • The reserved constructs $name and ( expression ) can be printed by escaping them with a \ character.
  • print $variable_name will print the value of the named variable.
  • print \$variable_name will print: $variable_name
  • print (3+4) will print the evaluated result of the contents of the brackets: 7
  • print \(3+4) will print: (3+4)
  • multi line print is achieved by wrapping the text in doubled curly brackets, e.g.:
    print {{ Print
    on
    multiple
    lines }}
  • { and } may need to be converted to their html entities depending on circumstances:
    { = { or {
    } = } or }
  • ; character cannot be the last character used of a print line
    The ; can be assigned to a variable using the chr function and then that is used in its place
  • print_raw will output the string unparsed Except for "get variables" and {{ }} wrappers

Learn different XtScript Print commands from examples

Use \ character to escaping variable.

- Code:
<!--parser:xtscript-->
	var $size = large

	print My house is $size<br/><br/>
	print <b>$size</b> is the value of \$size variable
<!--/parser:xtscript-->
- Result:
My house is large

large is the value of $size variable
Use \ character to escaping arithmetic operation

- Code:
<!--parser:xtscript-->
	var $money = 500
	var $spend = 150
	
	print My pocket money right now is ($money - $spend)<br/><br/>

	print The operation used to calculate my pocket money is: \($money - $spend)
<!--/parser:xtscript-->
- Result:
My pocket money right now is 350

The operation used to calculate my pocket money is: (500 - 150)
We can print and break strings into multiple lines if we put it inside curly brackets {{ }}

- Code:
<!--parser:xtscript-->
	print {{ <p>Break string into multiple lines can help make the code to be more readable.</p>
<p>Rather than using multiple prints command, 
We can put the string inside: 
&#123;&#123; &#125;&#125; (curly brackets)</p> }}
<!--/parser:xtscript-->
- Result:

Break string into multiple lines can help make the code to be more readable.

Rather than using multiple prints command, We can put the string inside: {{ }} (curly brackets)

We cannot print a semicolon (;) as the last character, but we can use XtScript chr function to assign a ; into a variable and print it as the last character

- Code
<!--parser:xtscript-->
	var $text = Let's see if the semicolon is printed behind this text

	# Assigning semicolon into a variable using chr function
	var $sem = call chr $val=59
	
	print Experiment 1: $text ;
	print <br/><br/>
	print Experiment 2: $text $sem
<!--/parser:xtscript-->
- Result:
Experiment 1: Let's see if the semicolon is printed behind this text

Experiment 2: Let's see if the semicolon is printed behind this text ;

We can learn more about chr and other XtScript predefined functions later in the next subjects.

Use print_raw to print unparsed line

- Code:
<!--parser:xtscript-->
	var $green = <span style="color:green">This is</span>
	var $blue = <span style="color:blue">some example of</span>
	var $red = <span style="color:red">a colorful text</span>

	print $green $blue $red<br/><br/>

	print But behind a simple text, there are XtScript variables that combined it behind the scene: <br/>
	print_raw $green $blue $red
<!--/parser:xtscript-->
- Result:
This is some example of a colorful text

But behind a simple text, there are XtScript variables that combine it behind the scene:
$green $blue $red