Server scripting
Silk code can be
integrated into any Text such as HTML/XML and config file. Normally we can embed it in HTML to generate dynamic web page like PHP/ASP. Silk
server pages(.ssp) contain HTML with embedded Silk code, which is
enclosed in special opening and closing tags <?silk and ?>.
Everything
outside of a pair of opening and closing tags is ignored by the Silk
parser which allows ssp file to have mixed content:
<!DOCTYPE html>
<html>
<body>
<h1>My first Silk page</h1>
<?silk
response.write("Hello World");
?>
</body>
</html>
Silk will skip the blocks where the condition is not met, even though they are outside of the open/close tags:
<!DOCTYPE html>
<html>
<body>
<?silk
hour=_fun("time_now")["hour"];
?>
<?silk if (hour<10) ?>
<h>Good morning.</h>
<?silk else {?>
<h>Hello,</h>
<br/>
<h>It's a nice day.</h>
<?silk } ?>
<?silk
//Silk code can also be embedded in the middle of an HTML opening tag as follows:
?>
<p<?silk if (hour>12){ ?> style="color: rgb(255, 0, 0);"<?silk }?>>Good afternoon.</p>
</body>
</html>
response is a class object in Silk scripting, which has a
property named content and a method named write, we can get content with the property, and write content with the method.
Silk
will return the content of response as the whole web page, which
contains the mixed HTML content and the content written by the write
method:
<!DOCTYPE html>
<html>
<body>
<h1>My Silk page</h1>
<?silk
hour=_fun("time_now")["hour"];
if (hour<10)
{
response.write("Good Morning!");
}
else if (hour<20)
{
response.write("Good Day!");
}
else
{
response.write("Good night!");
}
?>
</body>
</html>
If we run the above sample before 10 am, we will get the following web page:
<!DOCTYPE html>
<html>
<body>
<h1>My Silk page</h1>
Good Morning!
</body>
</html>
response can write any string content, including HTML tags, and we also need to use escape
character \ if necessary:
<!DOCTYPE html>
<html>
<body>
<?silk
response.write(sprintf("<h2>%s!</h2>","this is a title"));
?>
<?silk
response.write("<p style=\"color:#0000ff\">This text is styled with the style attribute!</p>");
?>
</body>
</html>
We can stop the script running by returning the content of response at anywhere, and the remaining content will be ignored:
<!DOCTYPE html>
<html>
<body>
<?silk
return response.content;
?>
<?silk
response.write("<p style=\"color:#0000ff\">This text is styled with the style attribute!</p>");
?>
</body>
</html>
We can only get the following broken content after running the above page file:
<!DOCTYPE html>
<html>
<body>
We can also ingnore the response object and return the string directly:
<!DOCTYPE html>
<html>
<body>
<?silk
return "Hello";
?>
</body>
</html>
We will get the following content after running the above page file:
The Silk Server Page file(.ssp) should be executed by calling the built-in function _fun("
runfile_tag", filename, outputFile, args)
filename
is the full path of script file, outputFile is the file in which Silk
can write the content, nomally we pass empty string if we do not write
the content in file.
args
is the arguments which will be passed to the script, args must be a
dictionary object and we can exchange data with script file by using it.
#!C:\Silk\Silk.exe
#include "cgi.si"
main()
{
printf("Content-type:text/html\n\n");//header
env=get_envs();
args={};
args["ENV"]=env;
html=_fun("runfile_tag","page.ssp","",args);
printf(html);
}
The above CGI program will execute the script file page.ssp, and return the HTML content to Apache.
Please refer to the CGI sample in installer for details.
There
is a sample named HttpServer in installer. HttpServer is a
multi-threads HTTP server written in Silk, and we can run and test Silk
server page in it without any other servers.