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

PHP with volt

I want to use php functions like str_word_count with volt. For example, Split the output of {{ posts.post_con['post_category'] }}, It will something like

category1 category2 category3

and create links something like

<a href="cat/category1">category1</a><br />
<a href="cat/category2">category2</a><br />
<a href="cat/category3">category3</a><br />


33.8k
edited Aug '14

Create a Component, register it in the bootstrap, and call it in the volt template like {{ myComponent.myFunction("myParam") }}. In the function, you will return a string that contains that HTML code (beware of autoescape, it will affect the returned string).

At least this is what I remember.

You can extend Volt ; see https://docs.phalcon.io/en/latest/reference/volt.html#extending-volt

Or, if you want a quick & dirty solution: simply use PHP in your volt template with php open/close tags ; <?php $foo = "bar"; ?> will allow you to use {{ foo }} as a volt variable.