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.

How to truncate data while displaying in volt?

i am sending pagination data from controller to view....on view i want to display truncated data like one of the filed caontains reference id which is of 12 digits but i want to display only first 6 digits of the id ...how to do it in volt.

You have to add substr or mb_substr to volt and use one of those.

Here is how to: https://docs.phalconphp.com/en/3.0.0/reference/volt.html#id1

Thanks..!!! and where i have to add the code in the controller or in Services.php



65.8k
Accepted
answer

Where you define the volt services, should be in your services file.

// View
$di->setShared('view', function() use ($di) {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir(__DIR__ . '/views/');
    $view->setLayoutsDir(__DIR__ . '/views/_layouts/');
    $view->setLayout('default');

    $view->registerEngines(['.phtml' => function($view, $di) {
        $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
        $options = [
            'compiledPath' => $di->getConfig()->site->path->cache . 'volt/backend/',
            'compiledExtension' => '.php',
            'compileAlways' => $di->getConfig()->debug,
        ];
        $volt->setOptions($options);

        // Extended functions
        $compiler = $volt->getCompiler();
        $compiler->addFunction('in_array', 'in_array');
        $compiler->addFunction('is_dir', 'is_dir');
        $compiler->addFunction('getimagesize', 'getimagesize');
        return $volt;
    }]);

    return $view;
});

Hi @jaikumar002 you can use format filter is an alias of sprintf

{{ myVar|format('%.6s')|e }}

If you want show only first 6 chars

Good luck