We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.phalcon.io/en/3.0.0/reference/volt.html#id1

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



93.7k
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



309
edited Jul '20

Note that format() is to be use the other way around:

{{ '%.6f'|format(myNumber) }}