Fraenkiman e544ed6d9a Smatry Release 4.4.1 on Feb-2024
Comparing changes: https://github.com/smarty-php/smarty/compare/v4.3.1...v4.4.1

It is noticeable that Smarty 4.3.1 does not officially support PHP 8.3. Is only supported with 4.4.0.

Remark:

During tests with Smarty 4.5.1, it was noticed that the following warning occurs:
Deprecated: Using the unregistered function "function_exists" in a template is deprecated and will be removed in a future version. Use Smarty::registerPlugin to explicitly register a custom modifier.

As of Smarty 5.X.X, templates must be revised again.
The Smarty release 5.0.2 is already officially available. However, integration into FlatPress is not entirely trivial.
2024-04-14 18:37:39 +02:00

2.6 KiB

Escaping Smarty parsing

It is sometimes desirable or even necessary to have Smarty ignore sections it would otherwise parse. A classic example is embedding Javascript or CSS code in a template. The problem arises as those languages use the { and } characters which are also the default delimiters for Smarty.

Note

A good practice for avoiding escapement altogether is by separating your Javascript/CSS into their own files and use standard HTML methods to access them. This will also take advantage of browser script caching. When you need to embed Smarty variables/functions into your Javascript/CSS, then the following applies.

In Smarty templates, the { and } braces will be ignored so long as they are surrounded by white space. This behavior can be disabled by setting the Smarty class variable $auto_literal to false.

Examples

<script>
   // the following braces are ignored by Smarty
   // since they are surrounded by whitespace
   function foobar {
    alert('foobar!');
   }
   // this one will need literal escapement
   {literal}
    function bazzy {alert('foobar!');}
   {/literal}
</script>

{literal}..{/literal} blocks are used for escaping blocks of template logic. You can also escape the braces individually with {ldelim}, {rdelim} tags or {$smarty.ldelim},{$smarty.rdelim} variables.

Smarty's default delimiters { and } cleanly represent presentational content. However, if another set of delimiters suit your needs better, you can change them with Smarty's $left_delimiter and $right_delimiter values.

Note

Changing delimiters affects ALL template syntax and escapement. Be sure to clear out cache and compiled files if you decide to change them.

<?php

$smarty->left_delimiter = '<!--{';
$smarty->right_delimiter = '}-->';

$smarty->assign('foo', 'bar');
$smarty->assign('name', 'Albert');
$smarty->display('example.tpl');

Where the template is:

Welcome <!--{$name}--> to Smarty
<script language="javascript">
  var foo = <!--{$foo}-->;
  function dosomething() {
    alert("foo is " + foo);
  }
  dosomething();
</script>