Difference between revisions of "Templates and Custom PHP Logic"

From WHMCS Documentation

 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
We strongly recommend that all custom PHP logic be performed via hooks.  Hooks are the only future-proof way of performing your own PHP logic at the time of page rendering.
+
If you want to include custom PHP logic in your customized templates, we recommend using hooks.
 
+
Historically, Smarty has always allowed you to define custom PHP logic directly within template files.  This has often been used by users and third party developers as a quick and convenient way of performing additional logic and defining additional template output.
+
<div class="docs-alert-warning">
 
+
We deprecated support for Smarty's legacy tags in WHMCS 6.0 and plan to permanently remove support in WHMCS 9.0. You '''must''' disable and remove them. For more information, see [[Eliminating Legacy Smarty Tags]].
However, as of Smarty 3, support for the {php} block has been removed, and we are only providing legacy support to ease the transition for developers and users who work with our platform and rely on this functionality.
 
 
 
<div class="docs-alert-info">
 
In WHMCS 6.0 and later, a Security setting "Allow Smarty PHP Tags" has been introduced in '''Setup > General Settings > Security''' which must be checked to enable the use of PHP tags. At the time of writing, this setting defaults to On.  In the future, this setting will be defaulted to '''Off'''.
 
 
</div>
 
</div>
 
+
Below is an example of how a hook can be used to perform additional logic and define template variables for use in client area template files.
+
== Hook Points ==
 
+
 +
Hook points for every page of the Client Area allow you to define system theme and order form template variables. For a full list, see our [https://developers.whmcs.com/hooks-reference/client-area-interface/ Client Area Interface Hooks Reference].
 +
 +
== Example Hook File ==
 +
 
 +
The following example demonstrates using a hook on the Client Area '''View Ticket''' page:
 +
 
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
Line 23: Line 25:
 
{
 
{
 
     $extraTemplateVariables = array();
 
     $extraTemplateVariables = array();
 
+
 
 
     // set a fixed value
 
     // set a fixed value
 
     $extraTemplateVariables['fixedValue'] = 'abc';
 
     $extraTemplateVariables['fixedValue'] = 'abc';
 
+
 
 
     // fetch clients data if available
 
     // fetch clients data if available
 
     $clientsData = isset($vars['clientsdetails']) ? $vars['clientsdetails'] : null;
 
     $clientsData = isset($vars['clientsdetails']) ? $vars['clientsdetails'] : null;
 
+
 
 
     // determine if client is logged in
 
     // determine if client is logged in
 
     if (is_array($clientsData) && isset($clientsData['id'])) {
 
     if (is_array($clientsData) && isset($clientsData['id'])) {
Line 37: Line 39:
 
         $extraTemplateVariables['anotherUserOnlyValue'] = '456';
 
         $extraTemplateVariables['anotherUserOnlyValue'] = '456';
 
     }
 
     }
 
+
 
 
     // return array of template variables to define
 
     // return array of template variables to define
 
     return $extraTemplateVariables;
 
     return $extraTemplateVariables;
 
}
 
}
 
+
 
add_hook('ClientAreaPage', 1, 'hook_template_variables_example');
+
add_hook('ClientAreaPageViewTicket', 1, 'hook_template_variables_example');
 
+
 
 +
</source>
 +
 
 +
The hook code above defines the additional system theme and order form template <tt>{$fixedValue}</tt> variable and, in the case of an authenticated user, the <tt>{$userSpecificValue}</tt> and <tt>{$anotherUserOnlyValue}</tt> variables.
 +
 +
You can them use them in the associated system theme and order form template file (in this case, <tt>viewticket.tpl</tt>):
 +
 
 +
<source lang="php">
 +
<p>The fixed value is {$fixedValue}.</p>
 +
 
 +
{if $userSpecificValue && $anotherUserOnlyValue}
 +
    <p>I also have this {$userSpecificValue} and {$anotherUserOnlyValue} to show you.</p>
 +
{/if}
 
</source>
 
</source>

Latest revision as of 21:49, 26 January 2023

If you want to include custom PHP logic in your customized templates, we recommend using hooks.

We deprecated support for Smarty's legacy tags in WHMCS 6.0 and plan to permanently remove support in WHMCS 9.0. You must disable and remove them. For more information, see Eliminating Legacy Smarty Tags.

Hook Points

Hook points for every page of the Client Area allow you to define system theme and order form template variables. For a full list, see our Client Area Interface Hooks Reference.

Example Hook File

The following example demonstrates using a hook on the Client Area View Ticket page:

<?php
/**
 * Hook sample for defining additional template variables
 *
 * @param array $vars Existing defined template variables
 *
 * @return array
 */
function hook_template_variables_example($vars)
{
    $extraTemplateVariables = array();
  
    // set a fixed value
    $extraTemplateVariables['fixedValue'] = 'abc';
  
    // 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
        $extraTemplateVariables['userSpecificValue'] = '123';
        $extraTemplateVariables['anotherUserOnlyValue'] = '456';
    }
  
    // return array of template variables to define
    return $extraTemplateVariables;
}
  
add_hook('ClientAreaPageViewTicket', 1, 'hook_template_variables_example');

The hook code above defines the additional system theme and order form template {$fixedValue} variable and, in the case of an authenticated user, the {$userSpecificValue} and {$anotherUserOnlyValue} variables.

You can them use them in the associated system theme and order form template file (in this case, viewticket.tpl):

<p>The fixed value is {$fixedValue}.</p>
  
{if $userSpecificValue && $anotherUserOnlyValue}
    <p>I also have this {$userSpecificValue} and {$anotherUserOnlyValue} to show you.</p>
{/if}