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 a replace returned values of model ?

hi guys! it's me again :) please, help me..

need replace returned values from model. i need get a short preview text in news. 10 words (example).

how a cut text in returned data as first 10 words?

$this->view->news = News::find('visible = 1');

sorry from my english dialect :( i hope you understand me...

edited Jul '15

Hi there... if i understand u right, u want to get some news and when u show them- to show truncated text. 1. So, u know how to get an article/news 2. U must print part of the content... in volt.

I will show u some way to do that: First: You need to make a plugin (helper) (not nessesery to follow my class names): to do that- u need to create a folder helpers (for example). Inside create file with name Volt.php that contain:


namespace Helpers;

class Volt
    static function truncateFilter($string, $length = 80, $etc = '...', $break_words = false, $middle = false)
        $charset = 'UTF-8';
        $utf8_modifier = 'u';
        if ($charset != 'UTF-8') {
            $utf8_modifier = '';

        if ($length == 0) return '';

        // Remove tags
        $string = strip_tags($string);

        // Go for it
        if (extension_loaded('mbstring')) {
            if (mb_strlen($string, $charset) > $length) {
                $length -= min($length, mb_strlen($etc, $charset));
                if (!$break_words && !$middle) {
                    $string = preg_replace('/\s+?(\S+)?$/' . $utf8_modifier, '', mb_substr($string, 0, $length + 1, $charset));
                if (!$middle) {
                    return mb_substr($string, 0, $length, $charset) . $etc;

                return mb_substr($string, 0, $length / 2, $charset) . $etc . mb_substr($string, - $length / 2, $length, $charset);

            return $string;

        // No MBString fallback
        if (isset($string[$length])) {
            $length -= min($length, strlen($etc));
            if (!$break_words && !$middle) {
                $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1));
            if (!$middle) {
                return substr($string, 0, $length) . $etc;

            return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2);

        return $string;

Next: You need to include this plugin/helper to your project in loader- just for example: /See Helpers/

            'Controllers' => __DIR__ . '/controllers/',
            'Helpers' => __DIR__ . '/helpers/'

Next: Give VOLT our extension: (example): /see Custom filter/

         * Setting up the view component
        $di->set('view', function () use ($config) {

            $view = new View();

                '.volt' => function ($view, $di) use ($config) {

                    $volt = new VoltEngine($view, $di);

                        'compileAlways' => $config->development, // This is mine development var xD
                        'compiledPath' => $config->application->appCacheDir."volt/",
                        'compiledSeparator' => '_'

                    // Custom filters
                    $volt->getCompiler()->addFilter('truncate', "\Helpers\Volt::truncateFilter");

                    return $volt;

            return $view;
        }, true);

So: we are almost ready :) If u make all parts work, u can use that helper in volt template like:

<p>{{ primary.content|truncate(90) }}</p>

...and u will got something like "text text text text text..." :)

I hope understand u well.

Good luck :)

One more thing - if u want- u can make model metods.. for example "getDescription()" and use \Helpers\Volt::truncateFilter($this->content)... just an idea

wow, it's a perfect! Спасибо Борис :)

when i used Twig (before meet phalcon) i use it hack for truncate phrases:

{{ news.content|split(' ')[0:10]|join(' ') }}

but him not working in volt.

Boris, i integrated u code in my project, all working, thanks again:)

edited Jul '15

U welcome :) And u did it. I just give an idea :)

About that {{ news.content|split(' ')[0:10]|join(' ') }} : u can make it work, just add split like custom filter/function in Volt- something like that :

$volt->getCompiler()->addFunction("split", "split");

However- never see Twig :/ i know i something similar, but... just use Twig template in NetBeans to color volt syntax :)

So... good luck and let the force be with u :) Aways

PS: пак заповядай :)