Compare commits

..

2 Commits

Author SHA1 Message Date
Simon Wisselink ed144d97bd add PHP8.3 JIT compiler to testsuite 2024-01-23 10:36:50 +01:00
Simon Wisselink 6218f395f4 Add php8.3 support. Fixes #925 2024-01-23 10:30:48 +01:00
42 changed files with 82 additions and 811 deletions
-4
View File
@@ -8,13 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- PHP8.3 support [#925](https://github.com/smarty-php/smarty/issues/925)
- Backlink to GitHub in docs
- Explain how to do escaping and set-up auto-escaping in docs [#865](https://github.com/smarty-php/smarty/issues/865)
- Link to variable scope page in the documentation for the assign tag [#878](https://github.com/smarty-php/smarty/issues/878)
### Fixed
- The {debug} tag was broken in v5 [#922](https://github.com/smarty-php/smarty/issues/922)
- Documentation on `{if $x is even by $y}` syntax
## [5.0.0-rc2] - 2023-11-11
+1 -1
View File
@@ -7,7 +7,7 @@ Smarty is a template engine for PHP, facilitating the separation of presentation
Read the [documentation](https://smarty-php.github.io/smarty/) to find out how to use it.
## Requirements
Smarty v5 can be run with PHP 7.2 to PHP 8.3.
Smarty v5 can be run with PHP 7.2 to PHP 8.2.
## Installation
Smarty versions 3.1.11 or later can be installed with [Composer](https://getcomposer.org/).
-1
View File
@@ -1 +0,0 @@
- Fix incorrect compilation of expressions when escape_html=true [#930](https://github.com/smarty-php/smarty/pull/930)
-1
View File
@@ -1 +0,0 @@
- Add support for implode, substr and json_encode as modifiers/functions in templates [#939](https://github.com/smarty-php/smarty/issues/939)
+1 -2
View File
@@ -89,5 +89,4 @@ Run this, and you will see:
```
Note how the [escape modifier](../designers/language-modifiers/language-modifier-escape.md)
translated the `&` character into the proper HTML syntax `&`.
Read more about auto-escaping in the [next section](./configuring.md).
translated the `&` character into the proper HTML syntax `&`.
-18
View File
@@ -122,24 +122,6 @@ $smarty->setCacheDir('/data/caches');
$cacheDir = $smarty->getCacheDir();
```
## Enabling auto-escaping
By default, Smarty does not escape anything you render in your templates. If you use
Smarty to render a HTML-page, this means that you will have to make sure that you do
not render any characters that have a special meaning in HTML, such as `&`, `<` and `>`,
or apply the [escape modifier](../designers/language-modifiers/language-modifier-escape.md)
to anything you want to render.
If you forget to do so, you may break your HTML page, or even create a vulnerability for
attacks known as [XSS or Cross Site Scripting](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html).
Luckily, you can tell Smarty to automatically apply the escape modifier to any dynamic part of your template.
It's like Smarty magically adds `|escape` to every variable you use on a web page.
Enable auto-escaping for HTML as follows:
```php
$smarty->setEscapeHtml(true);
```
## Disabling compile check
By default, Smarty tests to see if the
current template has changed since the last time
@@ -8,12 +8,12 @@ execution of a template**.
|----------------|------------|-----------------------------------------------------------------------|
| var | | The name of the variable being assigned |
| value | | The value being assigned |
| scope | (optional) | The scope of the assigned variable: 'parent','root' or 'global' |
| scope | (optional) | The scope of the assigned variable: \'parent\',\'root\' or \'global\' |
## Attributes of the {$var=...} syntax
| Attribute Name | Required | Description |
|----------------|------------|-----------------------------------------------------------------------|
| scope | (optional) | The scope of the assigned variable: 'parent','root' or 'global' |
| scope | (optional) | The scope of the assigned variable: \'parent\',\'root\' or \'global\' |
## Option Flags
| Name | Description |
@@ -102,8 +102,6 @@ A global variable is seen by all templates.
{$foo="bar" scope="global"}
```
For more information on variable scope, please read the page on [variable scopes](../language-variables/language-variable-scopes.md).
To access `{assign}` variables from a php script use
[`getTemplateVars()`](../../programmers/api-functions/api-get-template-vars.md).
Here's the template that creates the variable `$foo`.
@@ -252,7 +252,7 @@ iteration.
```smarty
{foreach $myNames as $name}
{if $name@index is even by 3}
{if $name@iteration is even by 3}
<span style="color: #000">{$name}</span>
{else}
<span style="color: #eee">{$name}</span>
@@ -478,13 +478,13 @@ header block every five rows.
</table>
```
An example that uses the `index` property to alternate a text color every
An example that uses the `iteration` property to alternate a text color every
third row.
```smarty
<table>
{section name=co loop=$contacts}
{if $smarty.section.co.index is even by 3}
{if $smarty.section.co.iteration is even by 3}
<span style="color: #ffffff">{$contacts[co].name}</span>
{else}
<span style="color: #dddddd">{$contacts[co].name}</span>
+55 -54
View File
@@ -6,10 +6,34 @@ or strings. To apply a modifier,
specify the value followed by a `|` (pipe) and the modifier name. A
modifier may accept additional parameters that affect its behavior.
These parameters follow the modifier name and are separated by a `:`
(colon).
(colon). Also, *all php-functions can be used as modifiers implicitly*
(more below) and modifiers can be
[combined](../language-combining-modifiers.md).
Modifiers can be applied to any type of variables, including arrays
and objects.
- [capitalize](language-modifier-capitalize.md)
- [cat](language-modifier-cat.md)
- [count_characters](language-modifier-count-characters.md)
- [count_paragraphs](language-modifier-count-paragraphs.md)
- [count_sentences](language-modifier-count-sentences.md)
- [count_words](language-modifier-count-words.md)
- [date_format](language-modifier-date-format.md)
- [default](language-modifier-default.md)
- [escape](language-modifier-escape.md)
- [from_charset](language-modifier-from-charset.md)
- [indent](language-modifier-indent.md)
- [lower](language-modifier-lower.md)
- [nl2br](language-modifier-nl2br.md)
- [regex_replace](language-modifier-regex-replace.md)
- [replace](language-modifier-replace.md)
- [spacify](language-modifier-spacify.md)
- [string_format](language-modifier-string-format.md)
- [strip](language-modifier-strip.md)
- [strip_tags](language-modifier-strip-tags.md)
- [to_charset](language-modifier-to-charset.md)
- [truncate](language-modifier-truncate.md)
- [unescape](language-modifier-unescape.md)
- [upper](language-modifier-upper.md)
- [wordwrap](language-modifier-wordwrap.md)
## Examples
@@ -41,63 +65,40 @@ and objects.
{* php's count *}
{$myArray|@count}
{* this will uppercase the whole array *}
{* this will uppercase and truncate the whole array *}
<select name="name_id">
{html_options output=$my_array|upper}
{html_options output=$my_array|upper|truncate:20}
</select>
```
- Modifiers can be applied to any type of variables, including arrays
and objects.
## Combining Modifiers
> **Note**
>
> The default behavior was changed with Smarty 3. In Smarty 2.x, you
> had to use an "`@`" symbol to apply a modifier to an array, such
> as `{$articleTitle|@count}`. With Smarty 3, the "`@`" is no
> longer necessary, and is ignored.
>
> If you want a modifier to apply to each individual item of an
> array, you will either need to loop the array in the template, or
> provide for this functionality inside your modifier function.
You can apply any number of modifiers to a variable. They will be
applied in the order they are combined, from left to right. They must be
separated with a `|` (pipe) character.
> **Note**
>
> Second, in Smarty 2.x, modifiers were applied to the result of
> math expressions like `{8+2}`, meaning that
> `{8+2|count_characters}` would give `2`, as 8+2=10 and 10 is two
> characters long. With Smarty 3, modifiers are applied to the
> variables or atomic expressions before executing the calculations,
> so since 2 is one character long, `{8+2|count_characters}`
> gives 9. To get the old result use parentheses like
> `{(8+2)|count_characters}`.
```php
<?php
$smarty->assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.');
```
where template is:
```smarty
{$articleTitle}
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."}
```
The above example will output:
```
Smokers are Productive, but Death Cuts Efficiency.
S M O K E R S A R ....snip.... H C U T S E F F I C I E N C Y .
s m o k e r s a r ....snip.... b u t d e a t h c u t s...
s m o k e r s a r e p r o d u c t i v e , b u t . . .
s m o k e r s a r e p. . .
```
## Using modifiers in expressions
Modifiers can also be used in expressions. For example, you can use the [isset modifier](./language-modifier-isset.md)
to test if a variable holds a value different from null.
```smarty
{if $varA|isset}
<b>variable A is set</b>
{/if}
```
You can also use modifiers in expressions in a PHP-style syntax:
```smarty
{if isset($varA)}
<b>variable A is set</b>
{/if}
```
- Custom modifiers can be registered
with the [`registerPlugin()`](../../programmers/api-functions/api-register-plugin.md)
function.
See also [`registerPlugin()`](../../programmers/api-functions/api-register-plugin.md), [combining
modifiers](../language-combining-modifiers.md). and [extending smarty with
@@ -1,21 +0,0 @@
# count
Returns the number of elements in an array (or Countable object). Will return 0 for null.
Returns 1 for any other type (such as a string).
If the optional mode parameter is set to 1, count() will recursively count the array.
This is particularly useful for counting all the elements of a multidimensional array.
## Basic usage
```smarty
{if $myVar|count > 3}4 or more{/if}
{if count($myVar) > 3}4 or more{/if}
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|--------------------------------------------------------|
| 1 | int | No | If set to 1, count() will recursively count the array. |
@@ -1,26 +0,0 @@
# debug_print_var
Returns the value of the given variable in a human-readable format in HTML.
Used in the [debug console](../chapter-debugging-console.md), but you can also use it in your template
while developing to see what is going on under the hood.
> **Note**
>
> Use for debugging only! Since you may accidentally reveal sensitive information or introduce vulnerabilities such as XSS using this
method never use it in production.
## Basic usage
```smarty
{$myVar|debug_print_var}
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|------------------------------------------------------------------------|
| 1 | int | No | maximum recursion depth if $var is an array or object (defaults to 10) |
| 2 | int | No | maximum string length if $var is a string (defaults to 40) |
@@ -1,11 +0,0 @@
# isset
Returns true if the variable(s) passed to it are different from null.
If multiple parameters are supplied then isset() will return true only if all of the parameters are
not null.
## Basic usage
```smarty
{if $myVar|isset}all set!{/if}
```
@@ -1,26 +0,0 @@
# join
Returns a string containing all the element of the given array
with the separator string between each.
## Basic usage
For `$myArray` populated with `['a','b','c']`, the following will return the string `abc`.
```smarty
{$myArray|join}
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|--------|----------|-------------------------------------------------------------|
| 1 | string | No | glue used between array elements. Defaults to empty string. |
## Examples
For `$myArray` populated with `[1,2,3]`, the following will return the string `1-2-3`.
```smarty
{$myArray|join:"-"}
```
@@ -1,27 +0,0 @@
# json_encode
Transforms a value into a valid JSON string.
## Basic usage
```smarty
{$user|json_encode}
```
Depending on the value of `$user` this would return a string in JSON-format, e.g. `{"username":"my_username","email":"my_username@smarty.net"}`.
## Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------------------------------------------------------------------------------------|
| 1 | int | No | bitmask of flags, directly passed to [PHP's json_encode](https://www.php.net/json_encode) |
## Examples
By passing `16` as the second parameter, you can force json_encode to always format the JSON-string as an object.
Without it, an array `$myArray = ["a","b"]` would be formatted as a javascript array:
```smarty
{$myArray|json_encode} # renders: ["a","b"]
{$myArray|json_encode:16} # renders: {"0":"a","1":"b"}
```
@@ -1,9 +0,0 @@
# noprint
Always returns an empty string. This can be used to call a function or a method on an object that
returns output, and suppress the output.
## Basic usage
```smarty
{$controller->sendEmail()|noprint}
```
@@ -1,32 +0,0 @@
# number_format
Allows you to format a number using decimals and a thousands-separator. By default, the number of decimals is 0
and the number is rounded.
## Basic usage
```smarty
{$num = 2000.151}
{$num|number_format} # renders: 2,000
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|--------|----------|---------------------------------------|
| 1 | int | No | number of decimals (defaults to 0) |
| 2 | string | No | decimal separator (defaults to ".") |
| 3 | string | No | thousands-separator (defaults to ",") |
## Examples
```smarty
{$num = 2000.151}
{$num|number_format:2} # renders: 2,000.15
```
```smarty
{$num = 2000.151}
{$num|number_format:2:".":""} # renders: 2000.15
```
@@ -1,35 +0,0 @@
# round
Rounds a number to the specified precision.
## Basic usage
```smarty
{3.14|round} # renders: 3
```
```smarty
{3.141592|round:2} # renders: 3.14
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|---------------------------|
| 1 | int | No | precision (defaults to 0) |
| 2 | int | No | mode (defaults to 1) |
If 'precision' is negative, the number is rounded to the nearest power of 10. See examples below.
The parameter 'mode' defines how the rounding is done. By default, 2.5 is rounded to 3, whereas 2.45 is rounded to 2.
You usually don't need to change this. For more details on rounding modes,
see [PHP's documentation on round](https://www.php.net/manual/en/function.round).
## Examples
By passing `16` as the second parameter, you can force json_encode to always format the JSON-string as an object.
Without it, an array `$myArray = ["a","b"]` would be formatted as a javascript array:
```smarty
{$myArray|json_encode} # renders: ["a","b"]
{$myArray|json_encode:16} # renders: {"0":"a","1":"b"}
```
@@ -1,32 +0,0 @@
# split
Splits a string into an array, using the optional second parameter as the separator.
## Basic usage
For `$chars` populated with `'abc'`, the following will produce a html list with 3 elements (a, b and c).
```smarty
<ol>
{foreach $chars|split as $char}
<li>{$char|escape}</li>
{/foreach}
</ol>
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|--------|----------|------------------------------------------------------------------------------------------------------------------------------|
| 1 | string | No | separator used to split the string on. Defaults to empty string, causing each character in the source string to be separate. |
## Examples
For `$ids` populated with `'1,2,3'`, the following will produce a html list with 3 elements (1, 2 and 3).
```smarty
<ol>
{foreach $ids|split:',' as $id}
<li>{$id|escape}</li>
{/foreach}
</ol>
```
@@ -1,14 +0,0 @@
# str_repeat
Repeats the given value n times.
## Basic usage
```smarty
{"hi"|str_repeat:2} # renders: hihi
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-----------------------|
| 1 | int | yes | number of repetitions |
@@ -1,9 +0,0 @@
# strlen
Returns the length (number of characters) in the given string, including spaces.
## Basic usage
```smarty
{"Smarty"|strlen} # renders: 6
{156|strlen} # renders: 3
```
@@ -1,25 +0,0 @@
# substr
Returns a part (substring) of the given string starting at a given offset.
## Basic usage
```smarty
{"Smarty"|substr:2} # renders: arty
{"Smarty"|substr:2:3} # renders: art
```
## Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-----------------------------------------------------|
| 1 | int | yes | offset (zero based, can be negative) |
| 2 | int | no | length of substring returned (unlimited of omitted) |
## Examples
When used with a negative offset, the substring starts n characters from the end of the string counting backwards.
```smarty
{"Smarty"|substr:-2} # renders: ty
{"Smarty"|substr:-2:1} # renders: t
```
@@ -29,5 +29,5 @@ If Strike isn't Settled Quickly it may Last a While.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
```
See also [`lower`](./language-modifier-lower.md) and
See also [`lower`](lower) and
[`capitalize`](language-modifier-capitalize.md).
+2 -18
View File
@@ -1,7 +1,7 @@
# Getting started
## Requirements
Smarty can be run with PHP 7.2 to PHP 8.3.
Smarty can be run with PHP 7.2 to PHP 8.2.
## Installation
Smarty can be installed with [Composer](https://getcomposer.org/).
@@ -86,7 +86,7 @@ needs to be located in the [`$template_dir`](./programmers/api-variables/variabl
```smarty
{* Smarty *}
<h1>Hello {$name|escape}, welcome to Smarty!</h1>
Hello {$name}, welcome to Smarty!
```
> **Note**
@@ -132,20 +132,6 @@ Now, run your PHP file. You should see *"Hello Ned, welcome to Smarty!"*
You have completed the basic setup for Smarty!
## Escaping
You may have noticed that the example template above renders the `$name` variable using
the [escape modifier](./designers/language-modifiers/language-modifier-escape.md). This
modifier makes string 'safe' to use in the context of an HTML page.
If you are primarily using Smarty for HTML-pages, it is recommended to enable automatic
escaping. This way, you don't have to add `|escape` to every variable you use on a web page.
Smarty will handle it automatically for you!
Enable auto-escaping for HTML as follows:
```php
$smarty->setEscapeHtml(true);
```
## Extended Setup
This is a continuation of the [basic installation](#installation), please read that first!
@@ -170,8 +156,6 @@ class My_GuestBook extends Smarty {
$this->setCompileDir('/web/www.example.com/guestbook/templates_c/');
$this->setConfigDir('/web/www.example.com/guestbook/configs/');
$this->setCacheDir('/web/www.example.com/guestbook/cache/');
$this->setEscapeHtml(true);
$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
$this->assign('app_name', 'Guest Book');
-4
View File
@@ -26,10 +26,6 @@ and 480 for $height, the result is:
- [Features](./features.md) - or "Why do I want Smarty?"
## Help
- [Search or create an issue](https://github.com/smarty-php/smarty/issues)
- [Upgrading from an older version](upgrading.md)
- [Some random tips & tricks](./appendixes/tips.md)
- [Troubleshooting](./appendixes/troubleshooting.md)
## Source code
- [Smarty repository at GitHub](https://github.com/smarty-php/smarty)
+1 -12
View File
@@ -49,40 +49,29 @@ nav:
- 'Introduction': 'designers/language-modifiers/index.md'
- 'capitalize': 'designers/language-modifiers/language-modifier-capitalize.md'
- 'cat': 'designers/language-modifiers/language-modifier-cat.md'
- 'count': 'designers/language-modifiers/language-modifier-count.md'
- 'count_characters': 'designers/language-modifiers/language-modifier-count-characters.md'
- 'count_paragraphs': 'designers/language-modifiers/language-modifier-count-paragraphs.md'
- 'count_sentences': 'designers/language-modifiers/language-modifier-count-sentences.md'
- 'count_words': 'designers/language-modifiers/language-modifier-count-words.md'
- 'date_format': 'designers/language-modifiers/language-modifier-date-format.md'
- 'debug_print_var': 'designers/language-modifiers/language-modifier-debug-print-var.md'
- 'default': 'designers/language-modifiers/language-modifier-default.md'
- 'escape': 'designers/language-modifiers/language-modifier-escape.md'
- 'from_charset': 'designers/language-modifiers/language-modifier-from-charset.md'
- 'indent': 'designers/language-modifiers/language-modifier-indent.md'
- 'isset': 'designers/language-modifiers/language-modifier-isset.md'
- 'join': 'designers/language-modifiers/language-modifier-join.md'
- 'json_encode': 'designers/language-modifiers/language-modifier-json-encode.md'
- 'lower': 'designers/language-modifiers/language-modifier-lower.md'
- 'noprint': 'designers/language-modifiers/language-modifier-noprint.md'
- 'number_format': 'designers/language-modifiers/language-modifier-number-format.md'
- 'nl2br': 'designers/language-modifiers/language-modifier-nl2br.md'
- 'regex_replace': 'designers/language-modifiers/language-modifier-regex-replace.md'
- 'replace': 'designers/language-modifiers/language-modifier-replace.md'
- 'round': 'designers/language-modifiers/language-modifier-round.md'
- 'spacify': 'designers/language-modifiers/language-modifier-spacify.md'
- 'split': 'designers/language-modifiers/language-modifier-split.md'
- 'str_repeat': 'designers/language-modifiers/language-modifier-str-repeat.md'
- 'string_format': 'designers/language-modifiers/language-modifier-string-format.md'
- 'strip': 'designers/language-modifiers/language-modifier-strip.md'
- 'strip_tags': 'designers/language-modifiers/language-modifier-strip-tags.md'
- 'strlen': 'designers/language-modifiers/language-modifier-strlen.md'
- 'substr': 'designers/language-modifiers/language-modifier-substr.md'
- 'to_charset': 'designers/language-modifiers/language-modifier-to-charset.md'
- 'truncate': 'designers/language-modifiers/language-modifier-truncate.md'
- 'unescape': 'designers/language-modifiers/language-modifier-unescape.md'
- 'upper': 'designers/language-modifiers/language-modifier-upper.md'
- 'wordwrap': 'designers/language-modifiers/language-modifier-wordwrap.md'
- 'Combining Modifiers': 'designers/language-combining-modifiers.md'
- 'Builtin Tags':
- 'Introduction': 'designers/language-builtin-functions/index.md'
- '{append}': 'designers/language-builtin-functions/language-function-append.md'
+1 -1
View File
@@ -8,7 +8,7 @@
COMPOSE_CMD="mutagen-compose"
$COMPOSE_CMD run --rm php72 ./run-tests.sh $@ && \
$COMPOSE_CMD run --rm php73 ./run-tests.sh $@ && \
$COMPOSE_CMD run --rm php74 ./run-tests.sh $@ && \
$COMPOSE_CMD run --rm php74 ./run-tests.sh $@ && \
$COMPOSE_CMD run --rm php80 ./run-tests.sh $@ && \
$COMPOSE_CMD run --rm php81 ./run-tests.sh $@ && \
@@ -1,14 +0,0 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty json_encode modifier plugin
*/
class JsonEncodeModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'json_encode(' . $params[0] . (isset($params[1]) ? ', (int) ' . $params[1] : '') . ')';
}
}
@@ -1,15 +0,0 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty substr modifier plugin
*/
class SubstrModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'substr((string) ' . $params[0] . ', (int) ' . $params[1] .
(isset($params[2]) ? ', (int) ' . $params[2] : '') . ')';
}
}
+5 -8
View File
@@ -43,19 +43,16 @@ class ModifierCompiler extends Base {
foreach ($parameter['modifierlist'] as $single_modifier) {
/* @var string $modifier */
$modifier = $single_modifier[0];
$modifier_params = array_values($single_modifier);
$modifier_params[0] = $output;
$params = implode(',', $modifier_params);
$single_modifier[0] = $output;
$params = implode(',', $single_modifier);
if (!is_object($compiler->getSmarty()->security_policy)
|| $compiler->getSmarty()->security_policy->isTrustedModifier($modifier, $compiler)
) {
if ($handler = $compiler->getModifierCompiler($modifier)) {
$output = $handler->compile($modifier_params, $compiler);
$output = $handler->compile($single_modifier, $compiler);
} elseif ($compiler->getSmarty()->getModifierCallback($modifier)) {
$output = sprintf(
'$_smarty_tpl->getSmarty()->getModifierCallback(%s)(%s)',
@@ -63,7 +60,7 @@ class ModifierCompiler extends Base {
$params
);
} elseif ($callback = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIERCOMPILER)) {
$output = (new \Smarty\Compile\Modifier\BCPluginWrapper($callback))->compile($modifier_params, $compiler);
$output = (new \Smarty\Compile\Modifier\BCPluginWrapper($callback))->compile($single_modifier, $compiler);
} elseif ($function = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIER)) {
if (!is_array($function)) {
$output = "{$function}({$params})";
+1 -1
View File
@@ -84,7 +84,7 @@ class PrintExpressionCompiler extends Base {
}
if ($compiler->getTemplate()->getSmarty()->escape_html) {
$output = "htmlspecialchars((string) ({$output}), ENT_QUOTES, '" . addslashes(\Smarty\Smarty::$_CHARSET) . "')";
$output = "htmlspecialchars((string) {$output}, ENT_QUOTES, '" . addslashes(\Smarty\Smarty::$_CHARSET) . "')";
}
}
+3 -1
View File
@@ -65,6 +65,8 @@ class Configfile extends BaseCompiler {
* @param Smarty $smarty global instance
*/
public function __construct(Smarty $smarty) {
$this->smarty = $smarty;
// get required plugins
$this->smarty = $smarty;
$this->config_data['sections'] = [];
$this->config_data['vars'] = [];
@@ -102,7 +104,7 @@ class Configfile extends BaseCompiler {
) . "\n",
$this
);
/* @var ConfigfileParser $this->parser */
$this->parser = new ConfigfileParser($this->lex, $this);
if ($this->smarty->_parserdebug) {
$this->parser->PrintTrace();
+1 -70
View File
@@ -28,7 +28,6 @@ class DefaultExtension extends Base {
case 'from_charset': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\FromCharsetModifierCompiler(); break;
case 'indent': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\IndentModifierCompiler(); break;
case 'isset': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\IssetModifierCompiler(); break;
case 'json_encode': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\JsonEncodeModifierCompiler(); break;
case 'lower': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\LowerModifierCompiler(); break;
case 'nl2br': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\Nl2brModifierCompiler(); break;
case 'noprint': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\NoPrintModifierCompiler(); break;
@@ -38,7 +37,6 @@ class DefaultExtension extends Base {
case 'strip': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\StripModifierCompiler(); break;
case 'strip_tags': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\StripTagsModifierCompiler(); break;
case 'strlen': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\StrlenModifierCompiler(); break;
case 'substr': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\SubstrModifierCompiler(); break;
case 'to_charset': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\ToCharsetModifierCompiler(); break;
case 'unescape': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\UnescapeModifierCompiler(); break;
case 'upper': $this->modifiers[$modifier] = new \Smarty\Compile\Modifier\UpperModifierCompiler(); break;
@@ -56,14 +54,11 @@ class DefaultExtension extends Base {
case 'debug_print_var': return [$this, 'smarty_modifier_debug_print_var'];
case 'escape': return [$this, 'smarty_modifier_escape'];
case 'explode': return [$this, 'smarty_modifier_explode'];
case 'implode': return [$this, 'smarty_modifier_implode'];
case 'join': return [$this, 'smarty_modifier_join'];
case 'mb_wordwrap': return [$this, 'smarty_modifier_mb_wordwrap'];
case 'number_format': return [$this, 'smarty_modifier_number_format'];
case 'regex_replace': return [$this, 'smarty_modifier_regex_replace'];
case 'replace': return [$this, 'smarty_modifier_replace'];
case 'spacify': return [$this, 'smarty_modifier_spacify'];
case 'split': return [$this, 'smarty_modifier_split'];
case 'truncate': return [$this, 'smarty_modifier_truncate'];
}
return null;
@@ -216,7 +211,7 @@ class DefaultExtension extends Base {
* > 1 would be returned, unless value was null, in which case 0 would be returned.
*/
if ($arrayOrObject instanceof \Countable || is_array($arrayOrObject)) {
if ($arrayOrObject instanceof Countable || is_array($arrayOrObject)) {
return count($arrayOrObject, (int) $mode);
} elseif ($arrayOrObject === null) {
return 0;
@@ -523,74 +518,10 @@ class DefaultExtension extends Base {
*/
public function smarty_modifier_explode($separator, $string, ?int $limit = null)
{
trigger_error("Using explode is deprecated. " .
"Use split, using the array first, separator second.", E_USER_DEPRECATED);
// provide $string default to prevent deprecation errors in PHP >=8.1
return explode($separator, $string ?? '', $limit ?? PHP_INT_MAX);
}
/**
* Smarty split modifier plugin
* Type: modifier
* Name: split
* Purpose: split a string by a string
*
* @param string $string
* @param string $separator
* @param int|null $limit
*
* @return array
*/
public function smarty_modifier_split($string, $separator, ?int $limit = null)
{
// provide $string default to prevent deprecation errors in PHP >=8.1
return explode($separator, $string ?? '', $limit ?? PHP_INT_MAX);
}
/**
* Smarty implode modifier plugin
* Type: modifier
* Name: implode
* Purpose: join an array of values into a single string
*
* @param array $values
* @param string $separator
*
* @return string
*/
public function smarty_modifier_implode($values, $separator = '')
{
trigger_error("Using implode is deprecated. " .
"Use join using the array first, separator second.", E_USER_DEPRECATED);
if (is_array($separator)) {
return implode((string) ($values ?? ''), (array) $separator);
}
return implode((string) ($separator ?? ''), (array) $values);
}
/**
* Smarty join modifier plugin
* Type: modifier
* Name: join
* Purpose: join an array of values into a single string
*
* @param array $values
* @param string $separator
*
* @return string
*/
public function smarty_modifier_join($values, $separator = '')
{
if (is_array($separator)) {
trigger_error("Using join with the separator first is deprecated. " .
"Call join using the array first, separator second.", E_USER_DEPRECATED);
return implode((string) ($values ?? ''), (array) $separator);
}
return implode((string) ($separator ?? ''), (array) $values);
}
/**
* Smarty wordwrap modifier plugin
* Type: modifier
@@ -103,8 +103,7 @@ class RegisterModifierTest extends PHPUnit_Smarty
public function dataUnknownModifiers(): array {
return [
['{" blah"|ltrim:" "}', 'blah'],
['{"blah"|strrev}', 'halb'],
['{"blah"|substr:1:2}', 'la'],
['{"blah"|ucfirst}', 'Blah'],
['{"blah"|md5}', md5('blah')],
];
@@ -103,37 +103,7 @@ class CompileIfTest extends PHPUnit_Smarty
array('{if 6 is not even}yes{else}no{/if}', 'no', 'IsNotEven', $i ++),
array('{if 3 is odd}yes{else}no{/if}', 'yes', 'IsOdd', $i ++),
array('{if 3 is not odd}yes{else}no{/if}', 'no', 'IsNotOdd', $i ++),
array('{if 0 is even by 3}yes{else}no{/if}', 'yes', 'IsEvenByTest0', $i ++),
array('{if 1 is even by 3}yes{else}no{/if}', 'yes', 'IsEvenByTest1', $i ++),
array('{if 2 is even by 3}yes{else}no{/if}', 'yes', 'IsEvenByTest2', $i ++),
array('{if 3 is even by 3}yes{else}no{/if}', 'no', 'IsEvenByTest3', $i ++),
array('{if 4 is even by 3}yes{else}no{/if}', 'no', 'IsEvenByTest4', $i ++),
array('{if 5 is even by 3}yes{else}no{/if}', 'no', 'IsEvenByTest5', $i ++),
array('{if 6 is even by 3}yes{else}no{/if}', 'yes', 'IsEvenByTest6', $i ++),
array('{if 7 is even by 3}yes{else}no{/if}', 'yes', 'IsEvenByTest7', $i ++),
array('{if 0 is odd by 3}yes{else}no{/if}', 'no', 'IsOddByTest0', $i ++),
array('{if 1 is odd by 3}yes{else}no{/if}', 'no', 'IsOddByTest1', $i ++),
array('{if 2 is odd by 3}yes{else}no{/if}', 'no', 'IsOddByTest2', $i ++),
array('{if 3 is odd by 3}yes{else}no{/if}', 'yes', 'IsOddByTest3', $i ++),
array('{if 4 is odd by 3}yes{else}no{/if}', 'yes', 'IsOddByTest4', $i ++),
array('{if 5 is odd by 3}yes{else}no{/if}', 'yes', 'IsOddByTest5', $i ++),
array('{if 6 is odd by 3}yes{else}no{/if}', 'no', 'IsOddByTest6', $i ++),
array('{if 7 is odd by 3}yes{else}no{/if}', 'no', 'IsOddByTest7', $i ++),
array('{if 2 is even by 3}yes{else}no{/if}', 'yes', 'IsEvenByVal1', $i ++),
array('{if 3 is even by 2}yes{else}no{/if}', 'no', 'IsEvenByVal2', $i ++),
array('{if 4 is even by 3}yes{else}no{/if}', 'no', 'IsEvenByVal3', $i ++),
array('{$foo=3}{if 2 is even by $foo}yes{else}no{/if}', 'yes', 'IsEvenByVar1', $i ++),
array('{$foo=2}{if 3 is even by $foo}yes{else}no{/if}', 'no', 'IsEvenByVar2', $i ++),
array('{$foo=3}{if 4 is even by $foo}yes{else}no{/if}', 'no', 'IsEvenByVar3', $i ++),
array('{if 2 is odd by 3}yes{else}no{/if}', 'no', 'IsOddByVal1', $i ++),
array('{if 3 is odd by 2}yes{else}no{/if}', 'yes', 'IsOddByVal2', $i ++),
array('{if 4 is odd by 3}yes{else}no{/if}', 'yes', 'IsOddByVal3', $i ++),
array('{$foo=3}{if 2 is odd by $foo}yes{else}no{/if}', 'no', 'IsOddByVar1', $i ++),
array('{$foo=2}{if 3 is odd by $foo}yes{else}no{/if}', 'yes', 'IsOddByVar2', $i ++),
array('{$foo=3}{if 4 is odd by $foo}yes{else}no{/if}', 'yes', 'IsOddByVar3', $i ++),
array('{$foo=3}{if 3 is odd by $foo}yes{else}no{/if}', 'yes', 'IsOddByVar', $i ++),
array('{$foo=3}{$bar=6}{if $bar is not odd by $foo}yes{else}no{/if}', 'yes', 'IsNotOddByVar', $i ++),
array('{$foo=3}{$bar=3}{if 3+$bar is not odd by $foo}yes{else}no{/if}', 'yes', 'ExprIsNotOddByVar', $i ++),
array('{$foo=2}{$bar=6}{if (3+$bar) is not odd by ($foo+1)}yes{else}no{/if}', 'no', 'ExprIsNotOddByExpr', $i ++),
@@ -15,10 +15,6 @@ class IssetTest extends \PHPUnit_Smarty {
$this->assertEquals("yay", $this->smarty->fetch("string:{if isset('')}yay{/if}"));
}
public function testEmptyStringIssetModifierSyntax() {
$this->assertEquals("yay", $this->smarty->fetch("string:{if ''|isset}yay{/if}"));
}
public function testFalseIsset() {
$this->smarty->assign('test', false);
$this->assertEquals("yay", $this->smarty->fetch("string:{if isset(\$test)}yay{/if}"));
@@ -18,7 +18,9 @@ class PluginModifierExplodeTest extends \PHPUnit_Smarty
}
/**
* @deprecated
* @return void
* @throws \Smarty\Exception
*
* @dataProvider explodeDataProvider
*/
public function testExplode($template, $subject, $expectedString)
@@ -1,61 +0,0 @@
<?php
/**
* Smarty PHPunit tests of modifier
*/
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;
class PluginModifierImplodeTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* @deprecated
*/
public function testDefault()
{
$tpl = $this->smarty->createTemplate('string:{$v|implode}');
$tpl->assign("v", ["1", "2"]);
$this->assertEquals("12", $this->smarty->fetch($tpl));
}
/**
* @deprecated
*/
public function testWithSeparator()
{
$tpl = $this->smarty->createTemplate('string:{$v|implode:","}');
$tpl->assign("v", ["a", "b"]);
$this->assertEquals("a,b", $this->smarty->fetch($tpl));
}
/**
* @deprecated
*/
public function testLegacyArgumentOrder()
{
$tpl = $this->smarty->createTemplate('string:{","|implode:$v}');
$tpl->assign("v", ["a", "b"]);
$this->assertEquals("a,b", $this->smarty->fetch($tpl));
}
/**
* @deprecated
*/
public function testInConditional()
{
$tpl = $this->smarty->createTemplate('string:{if implode($v) == "abc"}good{else}bad{/if}');
$tpl->assign("v", ['a','b','c']);
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
/**
* @deprecated
*/
public function testInConditionalWithSeparator()
{
$tpl = $this->smarty->createTemplate('string:{if implode($v, "-") == "a-b-c"}good{else}bad{/if}');
$tpl->assign("v", ['a','b','c']);
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
}
@@ -1,43 +0,0 @@
<?php
/**
* Smarty PHPunit tests of modifier
*/
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;
class PluginModifierJoinTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function testDefault()
{
$tpl = $this->smarty->createTemplate('string:{$v|join}');
$tpl->assign("v", ["1", "2"]);
$this->assertEquals("12", $this->smarty->fetch($tpl));
}
public function testWithSeparator()
{
$tpl = $this->smarty->createTemplate('string:{$v|join:","}');
$tpl->assign("v", ["a", "b"]);
$this->assertEquals("a,b", $this->smarty->fetch($tpl));
}
public function testInConditional()
{
$tpl = $this->smarty->createTemplate('string:{if join($v) == "abc"}good{else}bad{/if}');
$tpl->assign("v", ['a','b','c']);
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
public function testInConditionalWithSeparator()
{
$tpl = $this->smarty->createTemplate('string:{if join($v, "-") == "a-b-c"}good{else}bad{/if}');
$tpl->assign("v", ['a','b','c']);
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
}
@@ -1,72 +0,0 @@
<?php
/**
* Smarty PHPunit tests of modifier
*/
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;
class PluginModifierJsonEncodeTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* @dataProvider dataForDefault
*/
public function testDefault($value, $expected)
{
$tpl = $this->smarty->createTemplate('string:{$v|json_encode}');
$tpl->assign("v", $value);
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}
/**
* @dataProvider dataForDefault
*/
public function testDefaultAsFunction($value, $expected)
{
$tpl = $this->smarty->createTemplate('string:{json_encode($v)}');
$tpl->assign("v", $value);
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}
public function dataForDefault() {
return [
["abc", '"abc"'],
[["abc"], '["abc"]'],
[["abc",["a"=>2]], '["abc",{"a":2}]'],
];
}
/**
* @dataProvider dataForForceObject
*/
public function testForceObject($value, $expected)
{
$tpl = $this->smarty->createTemplate('string:{$v|json_encode:16}');
$tpl->assign("v", $value);
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}
/**
* @dataProvider dataForForceObject
*/
public function testForceObjectAsFunction($value, $expected)
{
$tpl = $this->smarty->createTemplate('string:{json_encode($v,16)}');
$tpl->assign("v", $value);
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}
public function dataForForceObject() {
return [
["abc", '"abc"'],
[["abc"], '{"0":"abc"}'],
[["abc",["a"=>2]], '{"0":"abc","1":{"a":2}}'],
];
}
}
@@ -1,53 +0,0 @@
<?php
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
/**
* class for modifier tests
*
*
*
*
*/
class PluginModifierSplitTest extends \PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
$this->smarty->registerPlugin('modifier', 'json_encode', 'json_encode');
}
/**
* @dataProvider explodeDataProvider
*/
public function testSplit($template, $subject, $expectedString)
{
$this->smarty->assign('subject', $subject);
$tpl = $this->smarty->createTemplate($template);
$res = $this->smarty->fetch($tpl);
$this->assertEquals($expectedString, $res);
}
public function explodeDataProvider()
{
return [
'default' => [
'template' => 'string:{$subject|split:","|json_encode}',
'subject' => 'a,b,c,d',
'expectedString' => '["a","b","c","d"]',
],
'withNoDelimiterFound' => [
'template' => 'string:{$subject|split:","|json_encode}',
'subject' => 'abcd',
'expectedString' => '["abcd"]',
],
'withNull' => [
'template' => 'string:{$subject|split:","|json_encode}',
'subject' => null,
'expectedString' => '[""]',
],
];
}
}
@@ -1,44 +0,0 @@
<?php
/**
* Smarty PHPunit tests of modifier
*/
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;
class PluginModifierSubstrTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function testDefault()
{
$tpl = $this->smarty->createTemplate('string:{$v|substr:1}');
$tpl->assign("v", "abc");
$this->assertEquals("bc", $this->smarty->fetch($tpl));
}
public function testTwoArguments()
{
$tpl = $this->smarty->createTemplate('string:{$v|substr:1:1}');
$tpl->assign("v", "abc");
$this->assertEquals("b", $this->smarty->fetch($tpl));
}
public function testNegativeOffset()
{
$tpl = $this->smarty->createTemplate('string:{$v|substr:-1}');
$tpl->assign("v", "abc");
$this->assertEquals("c", $this->smarty->fetch($tpl));
}
public function testInConditional()
{
$tpl = $this->smarty->createTemplate('string:{if substr($v, -1) == "c"}good{else}bad{/if}');
$tpl->assign("v", "abc");
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
}