Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Function to truncate strings


I'm searching for a built-in function to truncate strings but it doesn't seem to exist if I refer to the Volt documention.

So I was wondering if this kind of function actually exists in another component of the framework, or if not, how to add my own function to the existing Volt filters like nl2br, trim, etc ?

edited Oct '14

Hey man

You can adding function of php via view, for example

//  Setting up the view component
            function () use ($config) {
                $view = new View();
                $view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
                //@TODO: move to an extensions/filters/functions file.
                        '.volt' => function () use ($view, $config) {
                                $volt = new Volt($view);
                                        'compiledPath'      => $config->application->view->compiledPath,
                                        'compiledSeparator' => $config->application->view->compiledSeparator,
                                        'compiledExtension' => $config->application->view->compiledExtension,
                                        'compileAlways'     => $config->application->debug,
                                $compiler = $volt->getCompiler();
                                    function ($string) {
                                        return t($string);
                                $compiler->addFunction('array_key_exists', 'array_key_exists');
                                    function ($str, $maxLen = 35, $suffix = '...') {
                                        return 'Hovercrowd\Models\Tools::truncate(' . $str . ')';
                                return $volt;

                // Create an event manager
                $eventsManager = new EventsManager();

                // Attach a listener for type 'view'
                    function ($event, $view) {
                        if ($event->getType() == 'notFoundView') {
                            throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')');

                // Bind the eventsManager to the view component

                return $view;


Very convenient solution. I like this framework more and more :). Thank you for your help !



Can i ask more?

The code above is add function for volt template.

If i want to use function that is not class::function in php template. What i have to do?

For example: create __($string); function to call $translate->_($string); class->method