<!---
# This file is part of the ChillDev ViewHelpers bundle.
#
# @author Rafał Wrzeszcz <rafal.wrzeszcz@wrzasq.pl>
# @copyright 2013 - 2014 © by Rafał Wrzeszcz - Wrzasq.pl.
# @version 0.1.9
# @since 0.1.7
# @package ChillDev\Bundle\ViewHelpersBundle
-->

# Text truncating

Truncating strings is a very common task when presenting descriptions on website. **ChillDevViewHelpersBundle** comes with handy `truncate` helper that:

-   truncates the text if it's longer than expected,
-   appends suffix (customizable) to truncated text to denote that it's just a shortcut,
-   can truncate based on wourds bounds to avoid cutting words (works this way by default).

If input text fits in expected length helper does nothing with it.

It's as easy to use it:

```php
<?php echo $view['truncate']->truncate($yourLongText, $expectedLength); ?>
```

For instance:

```php
<?php echo $view['truncate']->truncate($content, 255); ?>
```

You can specify custom suffix if you don't like default one:

```php
<?php echo $view['truncate']->truncate($content, 255, '...'); ?>
```

By default `truncate` helper will respect words bounts to not cut them in parts - you can disable this behavior if you want by passing `false` as fourth parameter:

```php
<?php echo $view['truncate']->truncate($content, 255, '...', false); ?>
```

Truncate helper implements `__invoke()` magic method which allows you to use it in even more compact way:

```php
<?php echo $view['truncate']($text, 1024); ?>
```
