Using Hooks
It is recommended that all custom PHP logic be performed via hooks. At the time of page rendering, hooks are the only future-proof way of performing your own PHP logic
Historically, Smarty has proposed you to define custom PHP logic directly within system theme and order form template files. This has also often been used by users and third party developers to be used as rapid and convenient way of performing additional logic and defining additional system theme and order form template output.
However, as of Smarty 3, support for the {php} block has been removed, and we are only delivering legacy support to ease the transition for developers and users who work with our platform and rely on the very functionality.
The exposure of the template object via the legacy variable
$template
, available within the deprecated PHP blocks context, is only available iftemplate_object
is itemized in theenabled_special_smarty_vars
array as described in our Smarty Security Policy documentation.
Example
The example which is given below demonstrates how a hook can be used to perform additional PHP logic and define system theme and order form template variables for use in client area template files.
/*
* Hook sample for defining additional template variables
* @param array $vars Existing defined template variables
* @return array
*/
add_hook('ClientAreaPageViewTicket', 1, function($vars)
{
$newTemplateVariables = [];
// set a fixed value
$newTemplateVariables['newValue'] = 'xyz';
// fetch clients data if available
$clientsData = isset($vars['clientsdetails']) ? $vars['clientsdetails'] : null;
// determine if client is logged in
if (is_array($clientsData) && isset($clientsData['id'])) {
$userId = $clientsData['id'];
// perform calculation here
$newTemplateVariables['userSpecificValue'] = '1122';
$newTemplateVariables['anotherUserOnlyValue'] = '3344';
}
// return array of template variables to define
return $newTemplateVariables;
});
The above mentioned examples executes on the View Ticket page within the client area. There are hook points available for every page of the WHASOLS client area. They allow you to define template variables in this way. For a full list, visit the Client Area Interface Hooks Index Listing.
The above hook defines the {$newValue}
template variable and, in the case of a logged in user, the {$userSpecificValue}
and {$anotherUserOnlyValue}
variables. These can then be used inside the associated template file (in this case, viewticket.tpl
) in the regular way.
The fixed value is {$fixedValue}.
{if $userSpecificValue && $anotherUserOnlyValue}
I also have this {$userSpecificValue} and {$anotherUserOnlyValue} to show you.
{/if}