-
-
Notifications
You must be signed in to change notification settings - Fork 80
NPLServerPage
NPL server page is a mixed HTML/NPL file, usually with the extension .page
.
At runtime time, server page is preprocessed into pure NPL script and then executed. For example
<html><body>
<?npl for i=1,5 do ?>
<p>hello</p>
<?npl end ?>
</body></html>
Above server page will be first processed into following NPL page script
echo ("<html><body>");
for i=1,5 do
echo("<p>hello</p>")
end
echo ("</body></html>");
When running above page script, echo
command will generate the final HTML response text to be sent back to client.
When a HTTP request come and redirected to NPL page handler, a special sandbox environment table is created, all page scripts related to that request is executed in this newly created sandbox environment. So you can safely create global variables and expect them to be uninitialized for each page request.
However, the sandbox environment also have read/write access to the global per-thread NPL runtime environment, where all NPL classes are loaded.
In a page file, one can call NPL.load
to load a given NPL class, such as mysql.lua
; or one can also use the page command include
to load another page file into sandbox environment. The difference is that classes loaded by NPL.load
will be loaded only once per thread; where include
will be loaded for every HTTP request handled by a worker thread. Moreover, NPL web server will monitor file changes for all page files and recompile them when modified by a developer; for files with NPL.load
, you need to restart your server, or use special code to reload it.
The following commands can only be called from inside an NPL page file. They are shortcut to long commands.
Following objects and functions can be used inside page script:
request: current request object: headers and cookies
response: current response object: send headers or set cookies, etc.
echo(text): output html
__FILE__: current filename
page: the current page (parser) object
_GLOBAL: the _G itself
following are exposed via meta class:
include(filename, bReload): inplace include another script
include_once(filename): include only once, mostly for defining functions
print(...): output html with formatted string.
nplinfo(): output npl information.
exit(text), die(): end the request
dirname(__FILE__): get directory name
site_url(path, scheme):
addheader(name, value):
file_exists(filename):
log(obj)
sanitize(text) escape xml '<' '>'
json_encode(value) to json string
xml_encode(value) to xml string
include_pagecode(code, filename): inplace include page code.
get_file_text(filename)
see script/apps/WebServer/npl_page_env.lua
for detailed documentation.
Download Paracraft | ParacraftSDK | copyright by tatfook 2016 | upload image