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 service access

By reading documentation i found this feauture: Inject Services into a Template. there is written: If a service container (DI) is available for Volt.

How can i make DI available for Volt?



98.9k

DI is already enabled in Volt, you just have to access:

{{ di.get('someService').some }}

Yes, yes, but in documentation you specify access just by name of service. I think, there is a logic: if the variable does not exist, it is checked whether there is such a service. In documentation mistake?



98.9k

No, it's not a mistake, you can access any service by its name:

{{ url.get(['for': 'my-route']) }}
{{ flash.getMessages()|dump }}
{{ session.get("auth") }}

But Volt can also be used in stand-alone mode:

<?php

$compiler = new \Phalcon\Mvc\View\Engine\Volt\Compiler();
echo $compiler->compileString('{{ "hello" }}');

The code below does not have a DI available so you can't access services as variables or access the DI itself.