Files
smarty/docs/designers/language-modifiers/language-modifier-wordwrap.md

74 lines
2.1 KiB
Markdown
Raw Permalink Normal View History

2023-02-06 14:42:31 +01:00
# wordwrap
Wraps a string to a column width, the default is 80. As an optional
second parameter, you can specify a string of text to wrap the text to
the next line, the default is a carriage return `"\n"`. By default,
`wordwrap` will attempt to wrap at a word boundary. If you want to cut
off at the exact character length, pass the optional third parameter as
TRUE. This is equivalent to the PHP
2023-02-03 22:31:59 +01:00
[`wordwrap()`](https://www.php.net/wordwrap) function.
2023-02-06 14:42:31 +01:00
## Basic usage
```smarty
{$myVar|wordwrap:30}
```
2023-02-06 14:42:31 +01:00
## Parameters
2023-02-06 14:42:31 +01:00
| Parameter Position | Type | Required | Default | Description |
|--------------------|---------|----------|---------|-----------------------------------------------------------------------------------------------|
| 1 | integer | No | 80 | This determines how many columns to wrap to. |
| 2 | string | No | \\n | This is the string used to wrap words with. |
| 3 | boolean | No | FALSE | This determines whether to wrap at a word boundary (FALSE), or at the exact character (TRUE). |
2023-02-06 14:42:31 +01:00
## Examples
2023-02-06 14:42:31 +01:00
```php
<?php
2023-02-06 14:42:31 +01:00
$smarty->assign('articleTitle',
"Blind woman gets new kidney from dad she hasn't seen in years."
);
2023-02-06 14:42:31 +01:00
```
2023-02-06 14:42:31 +01:00
Where template is
2023-02-06 14:42:31 +01:00
```smarty
{$articleTitle}
2023-02-06 14:42:31 +01:00
{$articleTitle|wordwrap:30}
2023-02-06 14:42:31 +01:00
{$articleTitle|wordwrap:20}
2023-02-06 14:42:31 +01:00
{$articleTitle|wordwrap:30:"<br />\n"}
2023-02-06 14:42:31 +01:00
{$articleTitle|wordwrap:26:"\n":true}
```
Will output:
2023-02-06 14:42:31 +01:00
```html
Blind woman gets new kidney from dad she hasn't seen in years.
2023-02-06 14:42:31 +01:00
Blind woman gets new kidney
from dad she hasn't seen in
years.
2023-02-06 14:42:31 +01:00
Blind woman gets new
kidney from dad she
hasn't seen in
years.
2023-02-06 14:42:31 +01:00
Blind woman gets new kidney<br />
from dad she hasn't seen in<br />
years.
2023-02-06 14:42:31 +01:00
Blind woman gets new kidn
ey from dad she hasn't se
en in years.
```
2023-02-06 14:42:31 +01:00
See also [`nl2br`](language-modifier-nl2br.md) and
[`{textformat}`](../language-custom-functions/language-function-textformat.md).