Compare commits

...

46 Commits

Author SHA1 Message Date
Simon Wisselink d95c9b4850 array_map Variable to Variable::getValue in getTemplateVars.
Fixes #994
2024-04-13 10:43:21 +02:00
Simon Wisselink 177a3f4e6e Add test to reproduce error 2024-04-13 10:39:03 +02:00
kynx 77c0b74e3b Fix docblock types (#992) 2024-04-11 20:31:30 +02:00
Simon Wisselink 34adf4e54c Fixed unit tests to not rely on the existence of any domain or unavailability of internet access when running tests. (#987) 2024-04-06 23:41:20 +02:00
Simon Wisselink 5400b53edf Revert "fix release tooling to support/5 branch"
This reverts commit 569cef71d0.
2024-04-05 22:40:31 +02:00
Simon Wisselink 599bcee13e Fix Smarty::assign() not returning when called with an array as first parameter. (#973)
Fixes #972
2024-04-05 22:38:57 +02:00
Simon Wisselink 569cef71d0 fix release tooling to support/5 branch 2024-04-05 22:38:57 +02:00
Simon Wisselink 0972503aef version bump 2024-04-05 22:38:57 +02:00
Shad 46b15e6365 add is_array modifier doc (#984) 2024-04-03 19:57:09 +02:00
Simon Wisselink beafa5ec31 Merge branch 'release/5.0.1' 2024-03-27 22:57:07 +01:00
Simon Wisselink 82a815aafb version bump 2024-03-27 22:57:04 +01:00
Simon Wisselink 4efa427a87 Fix missing and bogus use lines in src/Smarty.php. (#970) 2024-03-27 22:55:23 +01:00
Wim Wisselink 30b1c5bf6d Deprecation fix for providing a non string value to ctype_digit (#960) 2024-03-27 22:55:23 +01:00
Markus Frühauf 3f871f9f7a Fix docblock information after Smarty 5 namespace changes (#968) 2024-03-27 22:55:23 +01:00
Simon Wisselink cafe5e1e59 Fix missing and bogus use lines in src/Smarty.php. (#970) 2024-03-27 22:53:03 +01:00
Wim Wisselink a58d869502 Deprecation fix for providing a non string value to ctype_digit (#960) 2024-03-27 11:48:38 +01:00
Markus Frühauf 28e11b114b Fix docblock information after Smarty 5 namespace changes (#968) 2024-03-27 11:47:42 +01:00
Simon Wisselink 1da30e76e8 Documented support for is in, added support for is not in. (#955)
* Documented support for `is in`, added support for `is not in`.
Fixes #937
* minor docs improvement
2024-03-25 13:54:02 +01:00
Simon Wisselink 58348c38ef changelog entry for depcreated stream variables 2024-03-25 13:48:53 +01:00
Simon Wisselink 578c03efa5 Deprecate stream variables (#933) 2024-03-25 13:45:04 +01:00
Simon Wisselink 7255b4d73c Internal compiler classes always return a string (#918)
* Internal compiler classes always return a string (the internal has_code flag has been removed for simplicity)
* Add :string method signature to compile method everywhere.
2024-03-25 13:44:06 +01:00
Simon Wisselink e161babbd4 Merge branch 'release/5.0.0' 2024-03-25 12:28:59 +01:00
Simon Wisselink f8e63fc480 version bump 2024-03-25 12:28:42 +01:00
Simon Wisselink 82397ec7f0 Fixed that scoped variables would overwrite parent scope. (#954)
* Fixed that scoped variables would overwrite parent scope.
Fixes #952

* Moved variable stack maintenance to methods and private properties in Data class.
2024-03-15 16:10:27 +01:00
Simon Wisselink 17da1f585e Fix Too many shorthand attributes error when using a modifier as a function with more than 3 parameters in an expression (#953)
Fixes #949
2024-03-15 10:26:17 +01:00
Simon Wisselink 293bc20db0 fix release tooling to auto-delete changelog files 2024-02-26 14:59:35 +01:00
Simon Wisselink 818b96ffbd re-order changelog 2024-02-26 14:38:31 +01:00
Simon Wisselink 7b5ec8a065 Merge branch 'release/5.0.0-rc3' 2024-02-26 14:36:56 +01:00
Simon Wisselink 4af5cc760d version bump 2024-02-26 14:36:56 +01:00
Simon Wisselink 41d80b99ac Implemented support for substr, implode and json_encode as modifiers. (#940)
* Implemented support for substr, implode and json_encode as modifiers. Fixes #939
* Added split and join in favor of explode and implode modifiers.
* Documented all available modifiers
2024-02-26 14:35:19 +01:00
Simon Wisselink 2b0ba0eabc Add template path to CompilerException to enable rich debug features (#936)
* Add template path to CompilerException to enable rich debug features
Fixes #935
2024-02-24 23:36:06 +01:00
Simon Wisselink 66edb56911 Fix PHP version for one of the local test runners. 7.4 was tested twice, 7.3 never. 2024-02-18 23:06:29 +01:00
Simon Wisselink 15e48b6af3 Merge branch 'wxiaoguang-fix-escape-5.0' 2024-02-05 14:11:44 +01:00
Simon Wisselink d5adea5863 added changelog 2024-02-05 14:11:16 +01:00
Simon Wisselink 8041d0a4e8 fixed ocumentation on {if is even by } syntax 2024-02-05 14:09:44 +01:00
Simon Wisselink 94d9861d29 added Link to variable scope page in the documentation for the assign tag
Fixes #878
2024-02-05 14:09:44 +01:00
Simon Wisselink 63ff7d81e0 Explain escaping and auto-escaping in the docs.
Fixes #865
2024-02-05 14:09:44 +01:00
Simon Wisselink e6b6a0cbb2 Add backlink to GitHub 2024-02-05 14:09:44 +01:00
Simon Wisselink 0912124c33 fixed ocumentation on {if is even by } syntax 2024-02-03 00:05:38 +01:00
Simon Wisselink c0306d9942 added Link to variable scope page in the documentation for the assign tag
Fixes #878
2024-02-02 23:16:51 +01:00
Simon Wisselink 3fff0813e8 Explain escaping and auto-escaping in the docs.
Fixes #865
2024-02-02 23:10:47 +01:00
Simon Wisselink 3714d9ad8d Add backlink to GitHub 2024-02-02 22:42:54 +01:00
wxiaoguang 1820e875dc fix escape 2024-01-26 15:44:59 +08:00
Simon Wisselink a112c7446c Update php version number in docs 2024-01-23 11:49:33 +01:00
Simon Wisselink aab7f3db71 Add php8.3 support (#926)
Fixes #925
2024-01-23 10:39:00 +01:00
Simon Wisselink 08c90664f0 Fixed failing {debug} tag. (#923)
Fixes #922
2024-01-01 22:41:09 +01:00
169 changed files with 2780 additions and 2071 deletions
+4
View File
@@ -31,6 +31,7 @@ jobs:
- "8.0"
- "8.1"
- "8.2"
- "8.3"
compiler:
- default
@@ -45,6 +46,9 @@ jobs:
- os: ubuntu-latest
php-version: "8.2"
compiler: jit
- os: ubuntu-latest
php-version: "8.3"
compiler: jit
steps:
- name: Checkout
+30 -1
View File
@@ -6,9 +6,38 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [5.0.0-rc2] - 2023-11-11
## [5.0.1] - 2024-03-27
- Fix error in Smarty\Smarty::compileAllTemplates() by including missing FilesystemIterator class [#966](https://github.com/smarty-php/smarty/issues/966)
## [5.0.0] - 2024-03-25
- Fixed that scoped variables would overwrite parent scope [#952](https://github.com/smarty-php/smarty/issues/952)
- Removed publicly accessible `$tpl->_var_stack` variable
### Fixed
- Too many shorthand attributes error when using a modifier as a function with more than 3 parameters in an expression [#949](https://github.com/smarty-php/smarty/issues/949)
### Removed
- Dropped support for undocumented `{time()}` added in v5.0.0 since we already have the documented `{$smarty.now}`
## [5.0.0-rc3] - 2024-02-26
### 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)
- Add support for implode, substr and json_encode as modifiers/functions in templates [#939](https://github.com/smarty-php/smarty/issues/939)
- Add template path to CompilerException to enable rich debug features [#935](https://github.com/smarty-php/smarty/issues/935)
### 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
- Fix incorrect compilation of expressions when escape_html=true [#930](https://github.com/smarty-php/smarty/pull/930)
## [5.0.0-rc2] - 2023-11-11
### Fixed
- Registered output filters wouldn't run [#899](https://github.com/smarty-php/smarty/issues/899)
- Use of negative numbers in {math} equations [#895](https://github.com/smarty-php/smarty/issues/895)
+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.2.
Smarty v5 can be run with PHP 7.2 to PHP 8.3.
## Installation
Smarty versions 3.1.11 or later can be installed with [Composer](https://getcomposer.org/).
-1
View File
@@ -29,5 +29,4 @@
## Unrelated / other
- review (and avoid) use of 'clone' keyword
- compiler->has_code seems silly. Why not have proper return values?
- what is 'user literal support', why are unit tests skipped?
+1
View File
@@ -0,0 +1 @@
- Internal compiler classes always return a string (the internal has_code flag has been removed for simplicity) [#918](https://github.com/smarty-php/smarty/pull/918)
+1
View File
@@ -0,0 +1 @@
- Using stream variables in templates now throws a deprecation notice [#933](https://github.com/smarty-php/smarty/pull/933)
+2
View File
@@ -0,0 +1,2 @@
- Documented support for `{if $element is in $array}` syntax [#937](https://github.com/smarty-php/smarty/issues/937)
- Added support for `{if $element is not in $array}` syntax [#937](https://github.com/smarty-php/smarty/issues/937)
+1
View File
@@ -0,0 +1 @@
- Fix Smarty::assign() not returning $this when called with an array as first parameter [#972](https://github.com/smarty-php/smarty/pull/972)
+1
View File
@@ -0,0 +1 @@
- Fix that getTemplateVars would return an array of objects instead of the assigned variables values [#994](https://github.com/smarty-php/smarty/issues/994)
+5 -1
View File
@@ -37,7 +37,11 @@ services:
service: base
build:
dockerfile: ./utilities/testrunners/php82/Dockerfile
php83:
extends:
service: base
build:
dockerfile: ./utilities/testrunners/php83/Dockerfile
volumes:
smarty-code:
+2 -1
View File
@@ -89,4 +89,5 @@ 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 `&`.
translated the `&` character into the proper HTML syntax `&`.
Read more about auto-escaping in the [next section](./configuring.md).
+18
View File
@@ -122,6 +122,24 @@ $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
+3
View File
@@ -10,4 +10,7 @@ template.
{$foo:bar}
```
NB. Support for using streams to call variables is deprecated since Smarty v5.1 and will be removed
in a future version.
See also [`Template Resources`](../resources.md)
@@ -27,6 +27,30 @@ Various basic operators can be applied directly to variable values.
> complex, it may be a good idea to move the bits that do not deal
> explicitly with presentation to PHP by way of plugins or modifiers.
## List
The following is a list of recognized operators, which must be
separated from surrounding elements by spaces. Note that items listed in
\[brackets\] are optional. PHP equivalents are shown where applicable.
| Operator | Alternates | Syntax Example | Meaning | PHP Equivalent |
|--------------------|------------|----------------------|--------------------------------|--------------------|
| == | eq | $a eq $b | equals | == |
| != | ne, neq | $a neq $b | not equals | != |
| > | gt | $a gt $b | greater than | > |
| < | lt | $a lt $b | less than | < |
| >= | gte, ge | $a ge $b | greater than or equal | >= |
| <= | lte, le | $a le $b | less than or equal | <= |
| === | | $a === 0 | check for identity | === |
| ! | not | not $a | negation (unary) | ! |
| % | mod | $a mod $b | modulo | % |
| is \[not\] div by | | $a is not div by 4 | divisible by | $a % $b == 0 |
| is \[not\] even | | $a is not even | \[not\] an even number (unary) | $a % 2 == 0 |
| is \[not\] even by | | $a is not even by $b | grouping level \[not\] even | ($a / $b) % 2 == 0 |
| is \[not\] odd | | $a is not odd | \[not\] an odd number (unary) | $a % 2 != 0 |
| is \[not\] odd by | | $a is not odd by $b | \[not\] an odd grouping | ($a / $b) % 2 != 0 |
| is in | | $a is in $b | exists in array | in_array($a, $b) |
| is \[not\] in | | $a is not in $b | does not exist in array | !in_array($a, $b) |
## Ternary
You can use the `?:` (or ternary) operator to test one expression and present the value
of the second or third expression, based on the result of the test.
@@ -88,9 +88,6 @@ Object chaining:
{$object->method1($x)->method2($y)}
Direct PHP function access:
{time()}
```
> **Note**
@@ -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,6 +102,8 @@ 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@iteration is even by 3}
{if $name@index is even by 3}
<span style="color: #000">{$name}</span>
{else}
<span style="color: #eee">{$name}</span>
@@ -3,32 +3,8 @@
`{if}` statements in Smarty have much the same flexibility as PHP
[if](https://www.php.net/if) statements, with a few added features for the
template engine. Every `{if}` must be paired with a matching `{/if}`.
`{else}` and `{elseif}` are also permitted. All PHP conditionals and
functions are recognized, such as *\|\|*, *or*, *&&*, *and*,
*is_array()*, etc.
The following is a list of recognized qualifiers, which must be
separated from surrounding elements by spaces. Note that items listed in
\[brackets\] are optional. PHP equivalents are shown where applicable.
## Qualifiers
| Qualifier | Alternates | Syntax Example | Meaning | PHP Equivalent |
|--------------------|------------|----------------------|--------------------------------|--------------------|
| == | eq | $a eq $b | equals | == |
| != | ne, neq | $a neq $b | not equals | != |
| > | gt | $a gt $b | greater than | > |
| < | lt | $a lt $b | less than | < |
| >= | gte, ge | $a ge $b | greater than or equal | >= |
| <= | lte, le | $a le $b | less than or equal | <= |
| === | | $a === 0 | check for identity | === |
| ! | not | not $a | negation (unary) | ! |
| % | mod | $a mod $b | modulo | % |
| is \[not\] div by | | $a is not div by 4 | divisible by | $a % $b == 0 |
| is \[not\] even | | $a is not even | \[not\] an even number (unary) | $a % 2 == 0 |
| is \[not\] even by | | $a is not even by $b | grouping level \[not\] even | ($a / $b) % 2 == 0 |
| is \[not\] odd | | $a is not odd | \[not\] an odd number (unary) | $a % 2 != 0 |
| is \[not\] odd by | | $a is not odd by $b | \[not\] an odd grouping | ($a / $b) % 2 != 0 |
`{else}` and `{elseif}` are also permitted. All [operators](../language-basic-syntax/language-syntax-operators.md) are recognized, such as *==*,
*\|\|*, *or*, *&&*, *and*, etc and you can use modifiers as functions, such as *is_array()*.
## Examples
```smarty
@@ -478,13 +478,13 @@ header block every five rows.
</table>
```
An example that uses the `iteration` property to alternate a text color every
An example that uses the `index` property to alternate a text color every
third row.
```smarty
<table>
{section name=co loop=$contacts}
{if $smarty.section.co.iteration is even by 3}
{if $smarty.section.co.index is even by 3}
<span style="color: #ffffff">{$contacts[co].name}</span>
{else}
<span style="color: #dddddd">{$contacts[co].name}</span>
@@ -3,31 +3,8 @@
`{while}` loops in Smarty have much the same flexibility as PHP
[while](https://www.php.net/while) statements, with a few added features for
the template engine. Every `{while}` must be paired with a matching
`{/while}`. All PHP conditionals and functions are recognized, such as
*\|\|*, *or*, *&&*, *and*, *is_array()*, etc.
The following is a list of recognized qualifiers, which must be
separated from surrounding elements by spaces. Note that items listed in
\[brackets\] are optional. PHP equivalents are shown where applicable.
## Qualifiers
| Qualifier | Alternates | Syntax Example | Meaning | PHP Equivalent |
|--------------------|------------|----------------------|--------------------------------|--------------------|
| == | eq | $a eq $b | equals | == |
| != | ne, neq | $a neq $b | not equals | != |
| > | gt | $a gt $b | greater than | > |
| < | lt | $a lt $b | less than | < |
| >= | gte, ge | $a ge $b | greater than or equal | >= |
| <= | lte, le | $a le $b | less than or equal | <= |
| === | | $a === 0 | check for identity | === |
| ! | not | not $a | negation (unary) | ! |
| % | mod | $a mod $b | modulo | % |
| is \[not\] div by | | $a is not div by 4 | divisible by | $a % $b == 0 |
| is \[not\] even | | $a is not even | \[not\] an even number (unary) | $a % 2 == 0 |
| is \[not\] even by | | $a is not even by $b | grouping level \[not\] even | ($a / $b) % 2 == 0 |
| is \[not\] odd | | $a is not odd | \[not\] an odd number (unary) | $a % 2 != 0 |
| is \[not\] odd by | | $a is not odd by $b | \[not\] an odd grouping | ($a / $b) % 2 != 0 |
`{/while}`. All [operators](../language-basic-syntax/language-syntax-operators.md) are recognized, such as *==*,
*\|\|*, *or*, *&&*, *and*, etc and you can use modifiers as functions, such as *is_array()*.
## Examples
```smarty
+54 -55
View File
@@ -6,34 +6,10 @@ 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). Also, *all php-functions can be used as modifiers implicitly*
(more below) and modifiers can be
[combined](../language-combining-modifiers.md).
(colon).
- [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)
Modifiers can be applied to any type of variables, including arrays
and objects.
## Examples
@@ -65,40 +41,63 @@ These parameters follow the modifier name and are separated by a `:`
{* php's count *}
{$myArray|@count}
{* this will uppercase and truncate the whole array *}
{* this will uppercase the whole array *}
<select name="name_id">
{html_options output=$my_array|upper|truncate:20}
{html_options output=$my_array|upper}
</select>
```
- Modifiers can be applied to any type of variables, including arrays
and objects.
> **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.
## Combining Modifiers
> **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}`.
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.
- Custom modifiers can be registered
with the [`registerPlugin()`](../../programmers/api-functions/api-register-plugin.md)
function.
```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}
```
See also [`registerPlugin()`](../../programmers/api-functions/api-register-plugin.md), [combining
modifiers](../language-combining-modifiers.md). and [extending smarty with
@@ -0,0 +1,21 @@
# 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. |
@@ -0,0 +1,26 @@
# 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) |
@@ -0,0 +1,9 @@
# is_array
Return true if the variable passed to it is an array.
## Basic usage
```smarty
{if $myVar|is_array}it's an array{/if}
```
@@ -0,0 +1,11 @@
# 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}
```
@@ -0,0 +1,26 @@
# 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:"-"}
```
@@ -0,0 +1,27 @@
# 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"}
```
@@ -0,0 +1,9 @@
# 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}
```
@@ -0,0 +1,32 @@
# 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
```
@@ -0,0 +1,35 @@
# 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"}
```
@@ -0,0 +1,32 @@
# 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>
```
@@ -0,0 +1,14 @@
# 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 |
@@ -0,0 +1,9 @@
# strlen
Returns the length (number of characters) in the given string, including spaces.
## Basic usage
```smarty
{"Smarty"|strlen} # renders: 6
{156|strlen} # renders: 3
```
@@ -0,0 +1,25 @@
# 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`](lower) and
See also [`lower`](./language-modifier-lower.md) and
[`capitalize`](language-modifier-capitalize.md).
+18 -2
View File
@@ -1,7 +1,7 @@
# Getting started
## Requirements
Smarty can be run with PHP 7.2 to PHP 8.2.
Smarty can be run with PHP 7.2 to PHP 8.3.
## 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 *}
Hello {$name}, welcome to Smarty!
<h1>Hello {$name|escape}, welcome to Smarty!</h1>
```
> **Note**
@@ -132,6 +132,20 @@ 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!
@@ -156,6 +170,8 @@ 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,6 +26,10 @@ 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
View File
@@ -117,6 +117,7 @@ The following constants have been removed to prevent global side effects.
- Smarty now always runs in multibyte mode. Make sure you use the [PHP multibyte extension](https://www.php.net/manual/en/book.mbstring.php) in production for optimal performance.
- Generated `<script>` tags lo longer have deprecated `type="text/javascript"` or `language="Javascript"` attributes
- Smarty will throw a compiler exception instead of silently ignoring a modifier on a function call, like this: `{include|dot:"x-template-id" file="included.dot.tpl"}`
- The ::getFile() method of a CompilerException will now return the full path of the template being compiled, if possible. This used to be 'file:relative_dir/filename.tpl'.
## Upgrading from v3 to v4
+1 -1
View File
@@ -12,7 +12,7 @@ git checkout -b "release/$1"
php utilities/update-changelog.php $1
php utilities/update-smarty-version-number.php $1
git add CHANGELOG.md src/Smarty.php
git add changelog CHANGELOG.md src/Smarty.php
git commit -m "version bump"
git checkout master
+13 -1
View File
@@ -49,29 +49,41 @@ 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'
- 'is_array': 'designers/language-modifiers/language-modifier-is_array.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'
+2 -1
View File
@@ -8,8 +8,9 @@
COMPOSE_CMD="mutagen-compose"
$COMPOSE_CMD run --rm php72 ./run-tests.sh $@ && \
$COMPOSE_CMD run --rm php74 ./run-tests.sh $@ && \
$COMPOSE_CMD run --rm php73 ./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 $@ && \
$COMPOSE_CMD run --rm php82 ./run-tests.sh $@
$COMPOSE_CMD run --rm php83 ./run-tests.sh $@
-3
View File
@@ -20,9 +20,6 @@ use Smarty\Template;
* - indent_char - string (" ")
* - wrap_boundary - boolean (true)
*
* @link https://www.smarty.net/manual/en/language.function.textformat.php {textformat}
* (Smarty online manual)
*
* @param array $params parameters
* @param string $content contents of the block
* @param Template $template template object
+2 -2
View File
@@ -226,8 +226,8 @@ abstract class Base implements CompilerInterface {
* @param Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return bool|string compiled code or true if no code has been compiled
* @return string compiled code as a string
* @throws \Smarty\CompilerException
*/
abstract public function compile($args, Template $compiler, $parameter = array(), $tag = null, $function = null);
abstract public function compile($args, Template $compiler, $parameter = array(), $tag = null, $function = null): string;
}
+2 -3
View File
@@ -50,7 +50,8 @@ class BlockCompiler extends Base {
* @throws CompilerException
* @throws Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if (!isset($tag[5]) || substr($tag, -5) !== 'close') {
$output = $this->compileOpeningTag($compiler, $args, $tag, $function);
@@ -77,7 +78,6 @@ class BlockCompiler extends Base {
);
}
$compiler->_cache['blockParams'][$compiler->_cache['blockNesting']]['callsChild'] = true;
$compiler->has_code = true;
$compiler->suppressNocacheProcessing = true;
$output = "<?php \n";
@@ -102,7 +102,6 @@ class BlockCompiler extends Base {
$compiler->getParser()->lex->taglineno
);
}
$compiler->has_code = true;
$compiler->suppressNocacheProcessing = true;
$output = "<?php \n";
+2 -2
View File
@@ -17,10 +17,10 @@ interface CompilerInterface {
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return bool|string compiled code or true if no code has been compiled
* @return string compiled code as a string
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null);
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string;
public function isCacheable(): bool;
}
@@ -27,7 +27,8 @@ class DefaultHandlerFunctionCallCompiler extends Base {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
+11 -14
View File
@@ -34,7 +34,7 @@ class FunctionCallCompiler extends Base {
*
* @var array
*/
protected $shorttag_order = ['var1', 'var2', 'var3'];
protected $shorttag_order = [];
/**
* Compiles code for the execution of a registered function
@@ -49,7 +49,8 @@ class FunctionCallCompiler extends Base {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
@@ -58,19 +59,15 @@ class FunctionCallCompiler extends Base {
$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
try {
$value = array_shift($_attr);
$output = $compiler->compileModifier([array_merge([$function], $_attr)], $value);
} catch (\Smarty\CompilerException $e) {
if ($functionHandler = $compiler->getSmarty()->getFunctionHandler($function)) {
// not cacheable?
$compiler->tag_nocache = $compiler->tag_nocache || !$functionHandler->isCacheable();
$output = "\$_smarty_tpl->getSmarty()->getFunctionHandler(" . var_export($function, true) . ")";
$output .= "->handle($_params, \$_smarty_tpl)";
} else {
throw $e;
}
if ($functionHandler = $compiler->getSmarty()->getFunctionHandler($function)) {
// not cacheable?
$compiler->tag_nocache = $compiler->tag_nocache || !$functionHandler->isCacheable();
$output = "\$_smarty_tpl->getSmarty()->getFunctionHandler(" . var_export($function, true) . ")";
$output .= "->handle($_params, \$_smarty_tpl)";
} else {
$compiler->trigger_template_error("unknown function '{$function}'", null, true);
}
if (!empty($parameter['modifierlist'])) {
@@ -11,8 +11,6 @@ namespace Smarty\Compile\Modifier;
* Input: string to catenate
* Example: {$var|cat:"foo"}
*
* @link https://www.smarty.net/manual/en/language.modifier.cat.php cat
* (Smarty online manual)
* @author Uwe Tews
*/
@@ -6,8 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: count_characters
* Purpose: count the number of characters in a text
*
* @link https://www.smarty.net/manual/en/language.modifier.count.characters.php count_characters (Smarty online
* manual)
* @author Uwe Tews
*/
@@ -6,8 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: count_paragraphs
* Purpose: count the number of paragraphs in a text
*
* @link https://www.smarty.net/manual/en/language.modifier.count.paragraphs.php
* count_paragraphs (Smarty online manual)
* @author Uwe Tews
*/
@@ -6,8 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: count_sentences
* Purpose: count the number of sentences in a text
*
* @link https://www.smarty.net/manual/en/language.modifier.count.paragraphs.php
* count_sentences (Smarty online manual)
* @author Uwe Tews
*/
@@ -6,7 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: count_words
* Purpose: count the number of words in a text
*
* @link https://www.smarty.net/manual/en/language.modifier.count.words.php count_words (Smarty online manual)
* @author Uwe Tews
*/
@@ -6,7 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: default
* Purpose: designate default value for empty variables
*
* @link https://www.smarty.net/manual/en/language.modifier.default.php default (Smarty online manual)
* @author Uwe Tews
*/
@@ -9,7 +9,6 @@ use Smarty\Exception;
* Name: escape
* Purpose: escape string for output
*
* @link https://www.smarty.net/docsv2/en/language.modifier.escape count_characters (Smarty online manual)
* @author Rodney Rehm
*/
@@ -6,7 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: indent
* Purpose: indent lines of text
*
* @link https://www.smarty.net/manual/en/language.modifier.indent.php indent (Smarty online manual)
* @author Uwe Tews
*/
@@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\CompilerException;
/**
* Smarty is_array modifier plugin
*/
class IsArrayModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (count($params) !== 1) {
throw new CompilerException("Invalid number of arguments for is_array. is_array expects exactly 1 parameter.");
}
return 'is_array(' . $params[0] . ')';
}
}
@@ -0,0 +1,14 @@
<?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] : '') . ')';
}
}
@@ -6,7 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: lower
* Purpose: convert string to lowercase
*
* @link https://www.smarty.net/manual/en/language.modifier.lower.php lower (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com>
* @author Uwe Tews
*/
@@ -7,7 +7,6 @@ namespace Smarty\Compile\Modifier;
* Name: nl2br
* Purpose: insert HTML line breaks before all newlines in a string
*
* @link https://www.smarty.net/docs/en/language.modifier.nl2br.tpl nl2br (Smarty online manual)
*/
class Nl2brModifierCompiler extends Base {
@@ -7,7 +7,6 @@ namespace Smarty\Compile\Modifier;
* Name: round
* Purpose: Returns the rounded value of num to specified precision (number of digits after the decimal point)
*
* @link https://www.smarty.net/docs/en/language.modifier.round.tpl round (Smarty online manual)
*/
class RoundModifierCompiler extends Base {
@@ -6,7 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: str_repeat
* Purpose: returns string repeated times times
*
* @link https://www.smarty.net/docs/en/language.modifier.str_repeat.tpl str_repeat (Smarty online manual)
*/
class StrRepeatModifierCompiler extends Base {
@@ -6,7 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: string_format
* Purpose: format strings via sprintf
*
* @link https://www.smarty.net/manual/en/language.modifier.string.format.php string_format (Smarty online manual)
* @author Uwe Tews
*/
@@ -9,7 +9,6 @@ namespace Smarty\Compile\Modifier;
* Example: {$var|strip} {$var|strip:"&nbsp;"}
* Date: September 25th, 2002
*
* @link https://www.smarty.net/manual/en/language.modifier.strip.php strip (Smarty online manual)
* @author Uwe Tews
*/
@@ -6,7 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: strip_tags
* Purpose: strip html tags from text
*
* @link https://www.smarty.net/docs/en/language.modifier.strip.tags.tpl strip_tags (Smarty online manual)
* @author Uwe Tews
*/
@@ -7,7 +7,6 @@ namespace Smarty\Compile\Modifier;
* Name: strlen
* Purpose: return the length of the given string
*
* @link https://www.smarty.net/docs/en/language.modifier.strlen.tpl strlen (Smarty online manual)
*/
class StrlenModifierCompiler extends Base {
@@ -0,0 +1,15 @@
<?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] : '') . ')';
}
}
@@ -6,7 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: lower
* Purpose: convert string to uppercase
*
* @link https://www.smarty.net/manual/en/language.modifier.upper.php lower (Smarty online manual)
* @author Uwe Tews
*/
@@ -6,7 +6,6 @@ namespace Smarty\Compile\Modifier;
* Name: wordwrap
* Purpose: wrap a string of text at a given length
*
* @link https://www.smarty.net/manual/en/language.modifier.wordwrap.php wordwrap (Smarty online manual)
* @author Uwe Tews
*/
+10 -8
View File
@@ -33,9 +33,8 @@ class ModifierCompiler extends Base {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
$compiler->has_code = true;
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$output = $parameter['value'];
@@ -43,16 +42,19 @@ class ModifierCompiler extends Base {
foreach ($parameter['modifierlist'] as $single_modifier) {
/* @var string $modifier */
$modifier = $single_modifier[0];
$single_modifier[0] = $output;
$params = implode(',', $single_modifier);
$modifier_params = array_values($single_modifier);
$modifier_params[0] = $output;
$params = implode(',', $modifier_params);
if (!is_object($compiler->getSmarty()->security_policy)
|| $compiler->getSmarty()->security_policy->isTrustedModifier($modifier, $compiler)
) {
if ($handler = $compiler->getModifierCompiler($modifier)) {
$output = $handler->compile($single_modifier, $compiler);
$output = $handler->compile($modifier_params, $compiler);
} elseif ($compiler->getSmarty()->getModifierCallback($modifier)) {
$output = sprintf(
'$_smarty_tpl->getSmarty()->getModifierCallback(%s)(%s)',
@@ -60,7 +62,7 @@ class ModifierCompiler extends Base {
$params
);
} elseif ($callback = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIERCOMPILER)) {
$output = (new \Smarty\Compile\Modifier\BCPluginWrapper($callback))->compile($single_modifier, $compiler);
$output = (new \Smarty\Compile\Modifier\BCPluginWrapper($callback))->compile($modifier_params, $compiler);
} elseif ($function = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIER)) {
if (!is_array($function)) {
$output = "{$function}({$params})";
+2 -1
View File
@@ -39,7 +39,8 @@ class ObjectMethodCallCompiler extends Base {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
+3 -4
View File
@@ -47,9 +47,8 @@ class PrintExpressionCompiler extends Base {
* @return string
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
$compiler->has_code = true;
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
@@ -84,7 +83,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) . "')";
}
}
+4 -3
View File
@@ -37,9 +37,8 @@ class SpecialVariableCompiler extends Base {
* @return string compiled code
* @throws CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
$compiler->has_code = true;
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$_index = preg_split("/\]\[/", substr($parameter, 1, strlen($parameter) - 2));
$variable = smarty_strtolower_ascii($compiler->getId($_index[0]));
@@ -129,5 +128,7 @@ class SpecialVariableCompiler extends Base {
}
return $compiled_ref;
}
return '';
}
}
+2 -2
View File
@@ -35,8 +35,8 @@ class Append extends Assign
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
{
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
+2 -2
View File
@@ -55,8 +55,8 @@ class Assign extends Base
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
{
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
$_nocache = false;
// check and get attributes
+2 -1
View File
@@ -24,7 +24,8 @@ class BCPluginWrapper extends Base {
/**
* @inheritDoc
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
return call_user_func($this->callback, $this->getAttributes($compiler, $args), $compiler->getSmarty());
}
}
+2 -1
View File
@@ -58,7 +58,7 @@ class Block extends Inheritance {
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
if (!isset($compiler->_cache['blockNesting'])) {
$compiler->_cache['blockNesting'] = 0;
@@ -87,5 +87,6 @@ class Block extends Inheritance {
$compiler->getParser()->current_buffer = new Template();
$compiler->getTemplate()->getCompiled()->setNocacheCode(false);
$compiler->suppressNocacheProcessing = true;
return '';
}
}
+1 -2
View File
@@ -18,7 +18,7 @@ class BlockClose extends Inheritance {
*
* @return bool true
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
[$_attr, $_nocache, $_buffer, $_has_nocache_code, $_className] = $this->closeTag($compiler, ['block']);
@@ -103,7 +103,6 @@ class BlockClose extends Inheritance {
if ($compiler->_cache['blockNesting'] === 0) {
unset($compiler->_cache['blockNesting']);
}
$compiler->has_code = true;
$compiler->suppressNocacheProcessing = true;
return $output;
}
+1 -1
View File
@@ -52,7 +52,7 @@ class BreakTag extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
[$levels, $foreachLevels] = $this->checkLevels($args, $compiler);
$output = "<?php ";
+2 -1
View File
@@ -47,7 +47,8 @@ class Call extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// save possible attributes
+3 -3
View File
@@ -53,7 +53,8 @@ class Capture extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$buffer = $_attr['name'] ?? "'default'";
@@ -66,7 +67,6 @@ class Capture extends Base {
$compiler->openTag('nocache');
}
$_output = "<?php \$_smarty_tpl->getSmarty()->getRuntime('Capture')->open(\$_smarty_tpl, $buffer, $assign, $append);?>";
return $_output;
return "<?php \$_smarty_tpl->getSmarty()->getRuntime('Capture')->open(\$_smarty_tpl, $buffer, $assign, $append);?>";
}
}
+2 -1
View File
@@ -29,7 +29,8 @@ class CaptureClose extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if (array_pop($compiler->_cache['capture_stack'])) {
// pop the virtual {nocache} tag from the stack.
+2 -1
View File
@@ -62,7 +62,8 @@ class ConfigLoad extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
+2 -1
View File
@@ -29,7 +29,8 @@ class Debug extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes, may trigger errors
$this->getAttributes($compiler, $args);
+2 -1
View File
@@ -22,7 +22,8 @@ class ElseIfTag extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $nocache_pushed] = $this->closeTag($compiler, ['if', 'elseif']);
+2 -1
View File
@@ -20,7 +20,8 @@ class ElseTag extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $compiler->tag_nocache] = $this->closeTag($compiler, ['if', 'elseif']);
$this->openTag($compiler, 'else', [$nesting, $compiler->tag_nocache]);
return '<?php } else { ?>';
+2 -1
View File
@@ -52,7 +52,8 @@ class EvalTag extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if (isset($_attr['assign'])) {
+2 -2
View File
@@ -52,7 +52,8 @@ class ExtendsTag extends Inheritance {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
@@ -86,7 +87,6 @@ class ExtendsTag extends Inheritance {
} else {
$this->compileEndChild($compiler, $_attr['file']);
}
$compiler->has_code = false;
return '';
}
+2 -1
View File
@@ -29,7 +29,8 @@ class ForClose extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
[$openTag, $nocache_pushed] = $this->closeTag($compiler, ['for', 'forelse']);
+2 -1
View File
@@ -21,7 +21,8 @@ class ForElse extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$tagName, $nocache_pushed] = $this->closeTag($compiler, ['for']);
$this->openTag($compiler, 'forelse', ['forelse', $nocache_pushed]);
return "<?php }} else { ?>";
+2 -1
View File
@@ -28,7 +28,8 @@ class ForTag extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
if ($parameter === 0) {
$this->required_attributes = ['start', 'to'];
+2 -1
View File
@@ -29,7 +29,8 @@ class ForeachClose extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
[$openTag, $nocache_pushed, $localVariablePrefix, $item, $restore] = $this->closeTag($compiler, ['foreach', 'foreachelse']);
+2 -1
View File
@@ -20,7 +20,8 @@ class ForeachElse extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$openTag, $nocache_pushed, $localVariablePrefix, $item, $restore] = $this->closeTag($compiler, ['foreach']);
$this->openTag($compiler, 'foreachelse', ['foreachelse', $nocache_pushed, $localVariablePrefix, $item, false]);
+2 -1
View File
@@ -79,7 +79,8 @@ class ForeachTag extends ForeachSection {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
// init
$this->isNamed = false;
+6 -5
View File
@@ -33,9 +33,10 @@ class FunctionClose extends Base {
* @param array $args array with attributes from parser
* @param object|\Smarty\Compiler\Template $compiler compiler object
*
* @return bool true
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->compiler = $compiler;
$saved_data = $this->closeTag($compiler, ['function']);
$_attr = $saved_data[0];
@@ -75,7 +76,7 @@ class FunctionClose extends Base {
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->assign(\$key, \$value);\n}\n";
$output .= "\$params = var_export(\$params, true);\n";
$output .= "echo \"/*%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/<?php ";
$output .= "\\\$_smarty_tpl->getSmarty()->getRuntime('TplFunction')->saveTemplateVariables(\\\$_smarty_tpl, '{$_name}');\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->assign(\\\$key, \\\$value);\n}\n?>";
$output .= "\\\$_smarty_tpl->pushStack();\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->assign(\\\$key, \\\$value);\n}\n?>";
$output .= "/*/%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/\";?>";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
@@ -86,7 +87,7 @@ class FunctionClose extends Base {
);
$compiler->getParser()->current_buffer->append_subtree($compiler->getParser(), $_functionCode);
$output = "<?php echo \"/*%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/<?php ";
$output .= "\\\$_smarty_tpl->getSmarty()->getRuntime('TplFunction')->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
$output .= "\\\$_smarty_tpl->popStack();?>\n";
$output .= "/*/%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/\";\n?>";
$output .= "<?php echo str_replace('{$compiler->getTemplate()->getCompiled()->nocache_hash}', \$_smarty_tpl->getCompiled()->nocache_hash ?? '', ob_get_clean());\n";
$output .= "}\n}\n";
@@ -140,7 +141,7 @@ class FunctionClose extends Base {
// restore old status
$compiler->getTemplate()->getCompiled()->setNocacheCode($saved_data[2]);
$compiler->getTemplate()->caching = $saved_data[3];
return true;
return '';
}
/**
+4 -3
View File
@@ -42,10 +42,11 @@ class FunctionTag extends Base {
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return bool true
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
@@ -67,6 +68,6 @@ class FunctionTag extends Base {
// Init temporary context
$compiler->getParser()->current_buffer = new \Smarty\ParseTree\Template();
$compiler->getTemplate()->getCompiled()->setNocacheCode(false);
return true;
return '';
}
}
+2 -1
View File
@@ -28,7 +28,8 @@ class IfClose extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $nocache_pushed] = $this->closeTag($compiler, ['if', 'else', 'elseif']);
+2 -1
View File
@@ -22,7 +22,8 @@ class IfTag extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
+2 -1
View File
@@ -67,7 +67,8 @@ class IncludeTag extends Base {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$uid = $t_hash = null;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
+2 -1
View File
@@ -30,7 +30,8 @@ class Ldelim extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', null, true);
+4 -5
View File
@@ -26,12 +26,11 @@ class Nocache extends Base {
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return bool
* @return string
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->openTag($compiler, 'nocache');
// this tag does not return compiled code
$compiler->has_code = false;
return true;
return '';
}
}

Some files were not shown because too many files have changed in this diff Show More