Description |
Includes the template specified by the file parameter.
Parameters:
- file - a string containing the file system path to the template to include.
- env - an optional table specifying the environment to be used by the included template. By default, the template will inherit its caller's environment.
Returns:
Notes:
This function is called by the web server to process the template specified by the requested URL. Subsequent invocations of this function can be used to include common or re-used code from other template files and will be included in-line where the cgilua.lp.include function is invoked in the originating template.
- During the processing of a web request, the local directory is temporarily changed to match the local directory of the path of the file being served, as determined by the URL of the request. This is usually different than the Hammerspoon default directory which corresponds to the directory which contains the
init.lua file for Hammerspoon.
The default template environment provides the following:
- the
__index metamethod points to the _G environment variable in the Hammerspoon Lua instance; this means that any global variable in the Hammerspoon environment is available to the lua code in a template file.
the __newindex metamethod points to a function which creates new "global" variables in the template files environment; this means that if a template includes another template file, and that second template file creates a "global" variable, that new variable will be available in the environment of the calling template, but will not be shared with the Hammerspoon global variable space; "global" variables created in this manner will be released when the HTTP request is completed.
print is overridden so that its output is streamed into the response body to be returned when the web request completes. It follows the traditional pattern of the print builtin function: multiple arguments are separated by a tab character, the output is terminated with a new-line character, non-string arguments are converted to strings via the tostring builtin function.
write is defined as an alternative to print and differs in the following ways from the print function described above: no intermediate tabs or newline are included in the output streamed to the response body.
cgilua is defined as a table containing all of the functions included in this support sub-module.
hsminweb is defined as a table which contains the following tables which may be of use:
- CGIVariables - a table containing key-value pairs of the same data available through the cgilua.servervariable function.
- id - a string, generated via
hs.host.globallyUniqueString , unique to this specific HTTP request.
- log - a table/object representing the
hs.httpserver.hsminweb instance of hs.logger . This can be used to log messages to the Hammerspoon console as described in the documentation for hs.logger .
- request - a table containing data representing the details of the HTTP request as it was made by the web client to the server. The following keys are commonly found:
- headers - a table containing key-value pairs representing the headers included in the HTTP request; unlike the values available through cgilua.servervariable or found in
CGIVariables , these are available in their raw form.
- this table also contains a table with the key "_". This table contains functions and data used internally, and is described more fully in a supporting document (TBD). It is targeted primarily at custom error functions designed for use with
hs.httpserver.hsminweb and should not generally be necessary for Lua template files.
- method - the method of the HTTP request, most commonly "GET" or "POST"
- path - the path portion of the requested URL.
- response - a table containing data representing the response being formed for the response to the HTTP request. This is generally handled for you by the
cgilua support functions, but for special cases, you can modify it directly; this should contain only the following keys:
- body - a string containing the response body. As the lua template outputs content, this string is appended to.
- code - an integer representing the currently expected response code for the HTTP request.
- headers - a table containing key-value pairs of the currently defined response headers
- _tmpfiles - used internally to track temporary files used in the completion of this HTTP request; do not modify directly.
|