> For the complete documentation index, see [llms.txt](https://docs.toomuchmedia.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.toomuchmedia.com/using-nats/one-off-articles/smarty/upgrading-from-smarty-2-to-smarty-4.md).

# Upgrading from Smarty 2 to Smarty 4

*Article Topics:* [*Smarty*](https://docs.toomuchmedia.com/getting-started/common-topics/smarty.md)

NATS uses Smarty 2 on PHP 7 and Smarty 4 on PHP 8, so upgrading your server to PHP 8 also moves your NATS installation to Smarty 4. Smarty 4 is not backward-compatible with Smarty 2. Templates and plugins that worked under Smarty 2 may behave differently, or stop loading entirely, after the upgrade.

This article covers what to prepare before the switch and the changes most likely to affect customized templates and plugins.

{% hint style="info" %}
**You do not need to change the templates provided by NATS**, meaning the **Base Site Templates (provided by NATS)** and the **Base Skin**. The notes below apply only to templates you have customized, such as **Default Site Templates (for all sites)**, site- or tour-specific templates, and templates in your own skins, and to custom Smarty plugins you have written.
{% endhint %}

## Before You Upgrade PHP

1. **Submit a support ticket.** Before upgrading PHP, [submit a support ticket](https://clients.toomuchmedia.com/) so we can make sure Smarty 4 is installed and enabled for your NATS installation.
2. **Make sure your custom Smarty plugins are PHP 8 compatible.** Plugins are PHP code and must run under PHP 8 as well as Smarty 4. See [Custom Smarty Plugins](#custom-smarty-plugins) below.
3. **Review your customized templates.** After the upgrade, check that each customized skin and site template still loads and displays as expected.

{% hint style="warning" %}
**This is not a complete list.** The sections below cover Smarty 4 changes we are aware of, but there may be others. We recommend reviewing and testing all of your customized templates and custom plugins after upgrading.
{% endhint %}

## Template Changes

### Empty templates

Any template that is completely empty causes an error when it loads and breaks the page. Add content to empty templates, or remove them, before upgrading.

### `{include_php}` and `{php}` tags

The `{include_php}` tag has been deprecated and removed from Smarty, and `{php}` tags are no longer supported. Templates that use either tag will fail to load. Move that PHP logic into a [custom Smarty plugin](https://docs.toomuchmedia.com/using-nats/one-off-articles/smarty/smarty-plugins.md) and call the plugin from the template instead. This keeps application code out of your templates.

### Math combined with modifiers

Math written directly inside a variable tag may not calculate correctly when a modifier is applied. For example:

```
{$total_profit/100|currency_format:2}
```

Use the `{math}` function to calculate the value first, then apply the modifier to the result:

```
{math equation="a/b" a=$total_profit b=100 assign="total_profit_final"}
{$total_profit_final|currency_format:2}
```

### Variables shared with sub-templates

Some variables assigned in one template are not visible to the sub-templates it includes. If a sub-template needs the variable, assign it with the `smarty` scope:

```
{assign var="folder" value="messages" scope="smarty"}
```

## Custom Smarty Plugins

### Function signature

The second parameter of a plugin function has changed. In Smarty 2, it was a reference to the Smarty object. In Smarty 4, it is the template object.

**Smarty 2:**

```php
function smarty_function_xxx($params, &$smarty) {
}
```

**Smarty 4:**

```php
function smarty_function_xxx(array $params, Smarty_Internal_Template $template) {
}
```

The file naming convention has not changed. A plugin named `xxx` still lives in `function.xxx.php` and is still called in a template as `{xxx}`.

### Assigning an array of variables

Passing an associative array to `assign()` no longer creates one template variable per key. Logic like this will not work with Smarty 4:

```php
$template->assign($condition_data);
```

Assign each key individually instead:

```php
if (is_array($condition_data)) {
    foreach ($condition_data as $key => $value) {
        $template->assign($key, $value);
    }
}
```

### Plugin location

Keep your custom plugins in the `plugins` directory in the root of your NATS installation (for example, `nats/plugins`). Avoid placing them in `nats/includes/smarty/plugins`. That folder belongs to the Smarty library itself, so plugins stored there stay behind with Smarty 2 when your installation moves to Smarty 4.

### Example Smarty 4 plugin

Create the file `nats/plugins/function.xxx.php`, replacing `xxx` with your function name:

```php
<?php
function smarty_function_xxx(array $params, Smarty_Internal_Template $template) {
    $a = "test";
    return $a;
}
```

Call it from any template:

```
{xxx}
```

This displays the word `test`.

## Related Articles

{% content-ref url="/pages/99IsvrScruoIu19EeD4L" %}
[NATS Version History & Compatibility](https://docs.toomuchmedia.com/setup/nats-version-history-and-compatibility.md)
{% endcontent-ref %}

{% content-ref url="/pages/KSR0XeaIDlax83Vd8p4t" %}
[Smarty Plugins](https://docs.toomuchmedia.com/using-nats/one-off-articles/smarty/smarty-plugins.md)
{% endcontent-ref %}

{% content-ref url="/pages/DyexLUz12Y1F7WXcccHc" %}
[Smarty](https://docs.toomuchmedia.com/using-nats/one-off-articles/smarty.md)
{% endcontent-ref %}
