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

Volt Filters examples, not all working ?

Hi, i've been reading the Volt page of the doc and i played with it today. However, when i ran accros the filter part with all the examples i've had some troubles :

{{ "\'this is a string\'"|stripslashes }}

gives me

Parse error: syntax error, unexpected 'this' (T_STRING) in C:\wamp\www\phalcon\app\views\index\index.volt.php on line 2
{{ [3, 1, 2]|sort }}

gives me

Notice: Array to string conversion in C:\wamp\www\phalcon\app\views\index\index.volt.php on line 12

BUT if i do {{ [3, 1, 2]|sort|var_dump }} with $compiler->addFilter('var_dump', 'var_dump');, it works of course, same goes for keys, so the example is not working but the functions are working for those 2

{{ ['first': 1, 'second': 2, 'third': 3]|keys }}

gives me

Notice: Array to string conversion in C:\wamp\www\phalcon\app\views\index\index.volt.php on line 8
Array
{{ '{"one":1,"two":2,"three":3}'|json_decode }}

gives me

Catchable fatal error: Object of class stdClass could not be converted to string in C:\wamp\www\phalcon\app\views\index\index.volt.php on line 1

However, {{ ["a","b"] | json_encode }} works

and

{{ "some\ntext"|nl2br }}

gives me

some\ntext, but when i do :

{{ "some (press enter)
text"|nl2br}} it works :)

Thoses examples come from : https://docs.phalcon.io/en/latest/reference/volt.html#filters For key and sort, it's just that the doc is giving us some wrong examples, and i'd be wise to change them :)

Did i misuse thoses filters or is anybody else having this problem ?



98.9k

All those return arrays or objects which can't be directly printed by 'echo':

{{ ['first': 1, 'second': 2, 'third': 3]|keys }
{{ '{"one":1,"two":2,"three":3}'|json_decode }}
{{ [3, 1, 2]|sort }}

It's like trying to do:

<?php echo array(1,2,3); ?>
<?php echo new stdClass(); ?>


2.9k
edited Oct '14

Not all of them, the first one isn't, is it ?

{{ "\'this is a string\'"|stripslashes }}

And i'm saying for the others examples that it'd be wise to change the examples in the doc to show the proper way to do it, in order to avoid confusion :)



2.9k
edited Oct '14

The examples have been edited they works now. However

{{ "\'this is a string\'"|stripslashes }}

is still giving me an error php Parse error: syntax error, unexpected 'this' (T_STRING) in C:\wamp\www\phalcon\app\views\index\index.volt.php on line 2