Difference between revisions of "Version 6 Template Migration Guide"

From WHMCS Documentation

(Created page with "=Smarty Templating Changes= WHMCS has gone through a number of backend changes in version 6. Included amongst them was internal upgrade from Smarty 2 to Smarty 3.1. Most cust...")
 
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Smarty Templating Changes=
+
WHMCS 6 includes an internal upgrade from Smarty 2 to Smarty 3.1. As a result, you will need to update any custom templates that use Smarty 2 features that are not available in Smarty 3.
 
+
WHMCS has gone through a number of backend changes in version 6. Included amongst them was internal upgrade from Smarty 2 to Smarty 3.1. Most custom templates won't need any updates to be compatible with version 6, but those that used features only available in Smarty 2 will need to be migrated to ensure a smooth transition to the latest version.
+
== {literal} Changes ==
 
+
<div class="docs-alert-info">The inverse of this change is also true. Custom templates made for WHMCS 6 that use Smarty 3 specific syntax are not compatible with previous versions of WHMCS.</div>
+
Smarty 3 changes the way in which templates interpret whitespace, removing the need for <tt>{literal}</tt> tags around <tt>{</tt> and <tt>}</tt> characters in CSS and JavaScript blocks.
 
+
=={literal} Changes==
+
For example:
 
+
Smarty 3's engine changes the way whitespace is interpreted in templates, removing the need for {literal} tags around "{" and "}" characters in CSS and Javascript blocks.
 
 
 
For example, the following Javascript from a Smarty template:  
 
 
 
 
<syntaxhighlight lang="smarty">
 
<syntaxhighlight lang="smarty">
 
<script type="text/javascript">
 
<script type="text/javascript">
     function example()  
+
     function example()
 
     {literal}{{/literal}
 
     {literal}{{/literal}
 
         alert('example');
 
         alert('example');
Line 19: Line 15:
 
</script>
 
</script>
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
Converts to:  
+
Converts to:
 
+
 
<syntaxhighlight lang="smarty">
 
<syntaxhighlight lang="smarty">
 
<script type="text/javascript">
 
<script type="text/javascript">
     function example()  
+
     function example()
 
     {
 
     {
 
         alert('example');
 
         alert('example');
Line 30: Line 26:
 
</script>
 
</script>
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
=={php} Block Syntax==
+
== {php} Block Syntax ==
 
+
<div class="docs-alert-info">An option was added to WHMCS 6 to enable or disable use of the potentially dangerous {php} tag in custom templates. This option is enabled by default but may become disabled by default in a future version. We recommend using hook functions over custom templates for backend interaction.</div>
+
<div class="docs-alert-warning">
 
+
We deprecated legacy Smarty tags in WHMCS 6.0 and will remove support entirely in WHMCS 9.0. You '''must''' remove these tags from your custom themes and templates. For more information, see [[Eliminating Legacy Smarty Tags]].
Smarty 3's backwards compatibility layer defines the {php} tag as a block plugin instead of a method native to the Smarty object. Combined with Smarty's internal rewrite in version 3, some syntax available within a Smarty 2 {php} block will need small modification
+
</div>
 
+
{| class="wikitable" |
+
In most cases, you can update a template's <tt>{php}</tt> block to a hook file. Hooks perform custom actions at specific points in WHMCS's page generation routines.
|-
+
! Original syntax
+
For example, you can reference existing template variables or define new template variables in a PHP file in the <tt>includes/hooks</tt> directory. Inspect and assign variables using the <tt>ClientAreaPage</tt> hook:
! New syntax
+
! Notes
 
|-
 
| <tt>$this</tt>
 
| <tt>$template</tt>
 
| All Smarty 3 block plugins have a <tt>$template</tt> parameter containing the current Smarty instance.
 
|-
 
| style="white-space: nowrap" | <tt>$this->_tpl_vars</tt>
 
| <tt>$template->getVariable('variableName')</tt> or <tt>$template->getTemplateVars()</tt>
 
| Template variables are represented by <tt>Smarty_Variable</tt> objects in Smarty 3, which have a value property.  
 
|-
 
|}
 
 
 
For example, the following {php} block:
 
 
 
<syntaxhighlight lang="php">
 
{php}
 
 
 
// Retrieve a single template variable.
 
$myValue = $this->_tpl_vars['myVariable'];
 
 
 
// Loop through all template variables.
 
foreach ($this->_tpl_vars as $key => $value) {
 
  echo "{$key}: {$value}";
 
}
 
 
 
// Assign a new template variable.
 
$this->assign('myNewVariable', 'myNewValue');
 
 
 
{/php}
 
</syntaxhighlight>
 
 
 
Converts to:
 
 
 
<syntaxhighlight lang="php">
 
{php}
 
 
 
// Retrieve a single template variable.
 
$myValue = $template->getVariable('myVariable')->value;
 
 
 
// Loop through all template variables.
 
foreach ($template->getTemplateVars() as $key => $variable) {
 
  echo "{$key}: {$variable->value}";
 
}
 
 
 
// The assign() method works as it did before, though it must now be
 
// called on the $template object instead of $this.
 
$template->assign('myNewVariable', 'myNewValue');
 
 
 
{/php}
 
</syntaxhighlight>
 
 
 
===Migrating From {php} Tags To Hooks===
 
 
 
It's a good practice to separate backend logic from display logic. In most cases a template's {php} block can be migrated to a hook file. Hooks are very useful for performing custom actions at specific points in WHMCS's page generation routines. See [[Action Hooks]] for more information.
 
 
 
For instance, one can reference existing template variables or define new template variables in a PHP file in the <tt>includes/hooks</tt> directory. Inspect and assign variables using the "ClientAreaPage" hook.
 
 
 
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 
add_hook('ClientAreaPage', 1, function($templateVariables)
 
add_hook('ClientAreaPage', 1, function($templateVariables)
Line 105: Line 44:
 
         doThings();
 
         doThings();
 
     }
 
     }
   
+
   
 
     // Define new variables to send to the template.
 
     // Define new variables to send to the template.
 
     return array(
 
     return array(
Line 112: Line 51:
 
});
 
});
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
And then reference the newly assigned variable in the template:
+
Then, reference the newly-assigned variable in the template:
 
+
 
<syntaxhighlight lang="smarty">
 
<syntaxhighlight lang="smarty">
 
<p>My custom variable value is: {$myCustomVariable}.</p>
 
<p>My custom variable value is: {$myCustomVariable}.</p>
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
=The $client Object=
+
For more information on using hooks in templates, see [[Templates and Custom PHP Logic]].
 
+
All client area templates now have a <tt>$client</tt> variable that represents the currently logged in client. <tt>$client</tt> is either a <tt>WHMCS\Client\User</tt> object or ''null'' if no client is logged in. See [[Using Models]] for more information on how to work with model-based objects.
+
== The $client Object ==
 
+
 +
All Client Area templates have a <tt>$client</tt> variable that represents the authenticated client. <tt>$client</tt> is either a <tt>WHMCS\Client\User</tt> object or ''null'' if no client is authenticated.
 +
 
<syntaxhighlight lang="smarty">
 
<syntaxhighlight lang="smarty">
 
{if $client === null}
 
{if $client === null}
Line 130: Line 71:
 
{/if}
 
{/if}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
 +
For more information, see [[Using Models]].

Latest revision as of 21:49, 26 January 2023

WHMCS 6 includes an internal upgrade from Smarty 2 to Smarty 3.1. As a result, you will need to update any custom templates that use Smarty 2 features that are not available in Smarty 3.

{literal} Changes

Smarty 3 changes the way in which templates interpret whitespace, removing the need for {literal} tags around { and } characters in CSS and JavaScript blocks.

For example:

<script type="text/javascript">
    function example()
    {literal}{{/literal}
        alert('example');
    {literal}}{/literal}
</script>

Converts to:

<script type="text/javascript">
    function example()
    {
        alert('example');
    }
</script>

{php} Block Syntax

We deprecated legacy Smarty tags in WHMCS 6.0 and will remove support entirely in WHMCS 9.0. You must remove these tags from your custom themes and templates. For more information, see Eliminating Legacy Smarty Tags.

In most cases, you can update a template's {php} block to a hook file. Hooks perform custom actions at specific points in WHMCS's page generation routines.

For example, you can reference existing template variables or define new template variables in a PHP file in the includes/hooks directory. Inspect and assign variables using the ClientAreaPage hook:

add_hook('ClientAreaPage', 1, function($templateVariables)
{
    // Look for an existing template variable.
    foreach ($templateVariables as $name => $variable) {
        doThings();
    }
     
    // Define new variables to send to the template.
    return array(
        'myCustomVariable' => 'myCustomValue',
    );
});

Then, reference the newly-assigned variable in the template:

<p>My custom variable value is: {$myCustomVariable}.</p>

For more information on using hooks in templates, see Templates and Custom PHP Logic.

The $client Object

All Client Area templates have a $client variable that represents the authenticated client. $client is either a WHMCS\Client\User object or null if no client is authenticated.

{if $client === null}
    <p>Nobody is logged in. :(</p>
{else}
    <p>Hello, {$client->firstName} {$client->lastName}!</p>
{/if}

For more information, see Using Models.