For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

Upgrading from Smarty 2 to Smarty 4

Article Topics: Smarty

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.

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.

Before You Upgrade PHP

  1. Submit a support ticket. Before upgrading PHP, submit a support ticket 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 below.

  3. Review your customized templates. After the upgrade, check that each customized skin and site template still loads and displays as expected.

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 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:

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

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:

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:

Smarty 4:

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:

Assign each key individually instead:

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:

Call it from any template:

This displays the word test.

NATS Version History & CompatibilitySmarty PluginsSmarty

Last updated

Was this helpful?