General WebTemplate Coding Rules
Some general rules for coding with WebTemplate
-
Avoid using hardcoded absolute file paths
don't use paths like '/data/www/webtemplate'
instead:
to reference the wtv2 directory use:
$GLOBALS["WTDIRECTORY"]
(it is currently set to /data/www/webtemplate/wtv2/)
to reference the webtemplate directory use:
$GLOBALS["WTSITEDIRECTORY"]
(it is currently set to /data/www/webtemplate/)
to reference a site's directory use:
$GLOBALS["WTSITEDIRECTORY"] . $GLOBALS["WT"]->getSiteSetting("Site Directory")
if you want to upload a file from a form to use temporarily, you can use
$GLOBALS["WT"]->uploadToTemp($fieldname);
this will return the path to the uploaded file (which will only exist on the server for 24 hrs)
Reason: the reason for not using hard coded file paths is the location of things may change if webtemplate is installed on another server
-
Avoid using SQL statments to access/modify WebTemplate data
Reason: WebTemplate needs to maintain the integrity of the tree.
-
Avoid using SQL in code outside of the /wtv2/code directory,
Instead you should be able to use:
$GLOBALS["WT"]->getNode()
$node->setAttributes()
$GLOBALS["WT"]->query()
eg:
$contact = $GLOBALS["WT"]->getNode("/Contacts/Graeme");
$contact->setAttributes(Array("Username" => "graeme"));
-
Avoid using $_REQUEST, $_POST and $_GET
Instead use
$GLOBALS["WT"]->getRequestValue($name, $default = "");
or
$GLOBALS["WT"]->getPostValue($name, $default = "");
Reason: these functions check if magic quotes are enabled and calls stripslashes if it is. Using them means code will be compatible on other php configurations.
You can also use
$GLOBALS["WT"]->requestValueExists($name)
to see if a value has been set
-
Avoid using PHP code in templates
In some cases PHP code will be necessary, but try to avoid using it in templates.
for most simple needs of PHP code there are wt tag equivalents, eg to test if a request variable is set
<wt:if test="[wt:wt.request.name]">
name has been set
</wt:if>
for more complicated needs of PHP code, the PHP code should be in a seperate file to the template (see elsewhere for an example)
Reason: Separating functionality (PHP code) and layout (WebTemplate Templates) is important for workflow and maintainability