Difference between revisions of "Templates and Custom PHP Logic"

From WHMCS Documentation

Line 1: Line 1:
We 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.
+
We recommend that you perform all custom PHP logic via hooks. Hooks are the only future-proof way of performing your own PHP logic at the time of page rendering.
 
   
 
   
Historically, Smarty has allowed you to define custom PHP logic directly within system theme and order form 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 system theme and order form template output.
+
Historically, Smarty has allowed you to define custom PHP logic directly within system theme and order form template files. This allowed for a quick 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 <tt>{php}</tt> 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.
+
However, Smarty 3 removed support for the <tt>{php}</tt> block. WHMCS now only provides legacy support to ease the transition for developers and users who rely on this functionality.
 
   
 
   
 
<div class="docs-alert-warning">
 
<div class="docs-alert-warning">
WHMCS 6.0 and later include a backwards compatibility '''Allow Smarty PHP Tags''' setting at '''Configuration (<i class="fa fa-wrench" aria-hidden="true"></i>) > System Settings > General Settings > Security''' or, prior to WHMCS 8.0, '''Setup > General Settings > Security'''. You must select this to enable the use of PHP tags. This setting defaults to '''Off''' and may be removed in a future version.
+
WHMCS 6.0 and later include a backwards compatibility '''Allow Smarty PHP Tags''' setting in the '''[[Security Tab|Security]]''' tab at '''Configuration (<i class="fa fa-wrench" aria-hidden="true"></i>) > System Settings > General Settings''' or, prior to WHMCS 8.0, '''Setup > General Settings'''.  
 +
* You '''must''' select this to enable the use of PHP tags.  
 +
* This setting defaults to '''Off''' and may be removed in a future version.
 
</div>
 
</div>
 
   
 
   
Line 14: Line 16:
 
   
 
   
 
<div class="docs-alert-info">
 
<div class="docs-alert-info">
The example below executes on the '''View Ticket''' page within the client area. There are hook points available for every page of the WHMCS client area. They allow you to define system theme and order form template variables. For a full list, see [https://developers.whmcs.com/hooks-reference/client-area-interface/ Client Area Interface Hooks Index Listing].
+
The example below executes on the '''View Ticket''' page within the Client Area. 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 [https://developers.whmcs.com/hooks-reference/client-area-interface/ Client Area Interface Hooks Index Listing].
 
</div>
 
</div>
 
   
 
   
Line 52: Line 54:
 
</source>
 
</source>
 
   
 
   
The above hook defines the additional system theme and order form template <tt>{$fixedValue}</tt> variable and, in the case of a logged-in user, <tt>{$userSpecificValue}</tt> and <tt>{$anotherUserOnlyValue}</tt> variables. You can them use them inside the associated system theme and order form template file (in this case, <tt>viewticket.tpl</tt>).
+
The hook code above defines the additional system theme and order form template <tt>{$fixedValue}</tt> variable and, in the case of a logged-in 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">
 
<source lang="php">

Revision as of 19:20, 27 April 2022

We recommend that you perform all custom PHP logic via hooks. Hooks are the only future-proof way of performing your own PHP logic at the time of page rendering.

Historically, Smarty has allowed you to define custom PHP logic directly within system theme and order form template files. This allowed for a quick and convenient way of performing additional logic and defining additional system theme and order form template output.

However, Smarty 3 removed support for the {php} block. WHMCS now only provides legacy support to ease the transition for developers and users who rely on this functionality.

WHMCS 6.0 and later include a backwards compatibility Allow Smarty PHP Tags setting in the Security tab at Configuration () > System Settings > General Settings or, prior to WHMCS 8.0, Setup > General Settings.

  • You must select this to enable the use of PHP tags.
  • This setting defaults to Off and may be removed in a future version.

Example Hook File

The below example demonstrates how a hook can be used to perform additional PHP logic and define variables for use in system theme and order form template files.

The example below executes on the View Ticket page within the Client Area. 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 Client Area Interface Hooks Index Listing.

<?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 a logged-in 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}